swift - 在 UIAlertView 中调整 UIPickerView 的大小

标签 swift uipickerview uialertview

我正在尝试将 UIPickerView 放在 UIAlertView 中,但我似乎无法正确调整它的大小。这是我得到的:

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)

最佳答案

诀窍是:

  • 使用多行消息为您的新 View 留出空间
  • 显示警报 View 时调整新 View 的大小

    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

关于swift - 在 UIAlertView 中调整 UIPickerView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41361177/

相关文章:

ios - 应用程序在后台运行时的 Firebase 数据库事务 iOS

iphone - 重新创建仅显示一行的 UIPickerView

ios - UIPickerView选定的RowInComponent :0 always return zero first when I try to set data in second component

objective-c - UIActivityIndi​​catorView 未在 UIAlertView 中显示 - iOS7

objective-c - 如何使 UIAlertView 不根据 buttonIndex 关闭

ios - 如何在 UIAlertController 中设置两个 UITextField 之间的距离?

xcode - 在 Swift 中发出 Amazon Web Services S3 POST 请求

uitableview - 您可以不使用 cellForRowAtIndexPath 方法来填充 UITableView 吗?

ios - 使物理体响应场但不响应其他物理体

ios - 通过 UITextFields 约束问题交换