繁体   English   中英

iOS警报视图控制器

[英]iOS alert view controller

let containerViewWidth = 250
let containerViewHeight = 120

let containerFrame=CGRect(x: 10, y: 70, width:  CGFloat(containerViewWidth), height: CGFloat(containerViewHeight))
let label=UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
label.text="anusha"

var containerView: UIView = UIView(frame: containerFrame);            
containerView.addSubview(label)
alert.view.addSubview(containerView)

            // now add some constraints to make sure that the alert resizes itself
let cons:NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.greaterThanOrEqual, toItem: containerView, attribute: NSLayoutAttribute.height, multiplier: 1.00, constant: 130)

 alert.view.addConstraint(cons)

 var cons2:NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.greaterThanOrEqual, toItem: containerView, attribute: NSLayoutAttribute.width, multiplier: 1.00, constant: 20)

  alert.view.addConstraint(cons2)
  alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.default, handler: nil))

添加视图后....关闭按钮不起作用...如何使此工作?

添加此行代码

containerView.isUserInteractionEnabled = false

因为你的containerView覆盖了你的Close Button

我已经尝试过您的代码,并且可以完全正常工作,

我刚刚添加了代码来展示它,

看一看

    let alert:UIAlertController = UIAlertController()
    let containerViewWidth = 250
    let containerViewHeight = 120

    let containerFrame=CGRect(x: 10, y: 70, width:  CGFloat(containerViewWidth), height: CGFloat(containerViewHeight))
    let label=UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    label.text="anusha"

    let containerView: UIView = UIView(frame: containerFrame);
    containerView.addSubview(label)
    alert.view.addSubview(containerView)

    // now add some constraints to make sure that the alert resizes itself
    let cons:NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.greaterThanOrEqual, toItem: containerView, attribute: NSLayoutAttribute.height, multiplier: 1.00, constant: 130)

    alert.view.addConstraint(cons)

    let cons2:NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.greaterThanOrEqual, toItem: containerView, attribute: NSLayoutAttribute.width, multiplier: 1.00, constant: 20)

    alert.view.addConstraint(cons2)
    alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.default, handler: nil))

    self.present(alert, animated: true, completion: nil)

只需再添加一个按钮即可关闭警报。
编辑一点代码:

alert.addAction(UIAlertAction(title: "Close", style: .UIAlertActionStyle.default, handler: { (action) in
        // Do some thing
        print("Do")
      }))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM