2016-12-28 43 views
4

我试图把UIPickerView放在UIAlertView但是我似乎无法正确调整它的大小。这里就是我得到:调整UIAlertView内的UIPickerView

enter image description here

这里是我的代码:

let alertView = UIAlertController(title: "Select item from list", message: "", preferredStyle: UIAlertControllerStyle.alert) 

    let pickerView = UIPickerView(frame: CGRect(x: 0, y: 0, width: 250, height: 60)) 
    pickerView.dataSource = self 
    pickerView.delegate = self 

    alertView.view.addSubview(pickerView) 

    let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil) 

    alertView.addAction(action) 
    parent.present(alertView, animated: true, completion: nil) 
+0

添加更多的高度选择器,60是不够的,尝试300或smthing像 –

+1

你有没有检查这一个http://stackoverflow.com/a/40191156/6433023 –

回答

5

的诀窍是:

  • 使用多条线路的消息给空间,为您的新观点
  • 调整提示视图时新视图的大小

    let alertView = UIAlertController(
        title: "Select item from list", 
        message: "\n\n\n\n\n\n\n\n\n", 
        preferredStyle: .alert) 
    
    let pickerView = UIPickerView(frame: 
        CGRect(x: 0, y: 50, width: 260, height: 162)) 
    pickerView.dataSource = self 
    pickerView.delegate = self 
    
    // comment this line to use white color 
    pickerView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.2) 
    
    alertView.view.addSubview(pickerView) 
    
    let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil) 
    
    alertView.addAction(action) 
    present(alertView, animated: true, completion: { _ in 
        pickerView.frame.size.width = alertView.view.frame.size.width 
    }) 
    

enter image description here

+0

这会在appstore中被拒绝吗? –

+1

@VlpplpRyguy我对.actionSheet风格有过这种把戏,并且没有从App Store中被拒绝。 – bubuxu