在 UIAlertView 中调整 UIPickerView 的大小 [英] Sizing a UIPickerView inside a UIAlertView

查看:26
本文介绍了在 UIAlertView 中调整 UIPickerView 的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to put a UIPickerView in a UIAlertView however I can't seem to size it correctly. Here's what I'm getting:

这是我的代码:

    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)

推荐答案

诀窍是:

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

  • use multiple lines for message to give space for your new view
  • adjust the size of the new view when the alert view is presented

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
})

这篇关于在 UIAlertView 中调整 UIPickerView 的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆