【问题标题】:Adding custom UIView to UIAlertController将自定义 UIView 添加到 UIAlertController
【发布时间】:2018-05-17 00:08:08
【问题描述】:

我有包含单选按钮的自定义 xib。 我正在尝试将此 xib 作为子视图添加到 alertcontroller 中。 但是如您所见,文本正在从 uiview 中输出:

let alert = Bundle.main.loadNibNamed("MyXib", owner: self, options: nil)!.last as! UIView

    let alertController = UIAlertController(title: "\n\n\n\n\n\n", message: nil, preferredStyle: .alert)
    let margin:CGFloat = 8.0

    let rect = CGRect(x: margin, y: margin, width: alertController.view.bounds.size.width - margin * 4, height: 300)

    let customViewForAlert = UIView(frame: rect)
    customViewForAlert.addSubview(alert)

    alertController.view.addSubview(customViewForAlert)
    self.present(alertController, animated: true, completion: nil)

我该如何解决? 谢谢

【问题讨论】:

  • 如答案中所述 - 你不能。但您可以 做两件事: (1) 将您的 xib 文件用作模态演示视图控制器。 (2) 在弹出窗口中使用它 - 但请注意,在 iPhone 上,您必须添加一些额外的代码才能做到这一点。
  • 你可以在一定程度上做到这一点,但你不应该这样做。更多信息请参考此链接developer.apple.com/documentation/uikit/uialertcontroller

标签: ios swift uiview xib


【解决方案1】:

来自 Apple 的文档:

UIAlertController 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。

请创建自定义视图,向其中添加内容,并将其显示在您的视图控制器中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多