iOS警报视图控制器

时间:2017-02-17 07:34:27

标签: ios iphone swift

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

添加视图后....关闭按钮无法正常工作...如何使其工作?

3 个答案:

答案 0 :(得分:0)

添加此行代码

containerView.isUserInteractionEnabled = false

因为您containerView覆盖了Close Button

答案 1 :(得分:0)

我已经尝试过您的代码,但它完全有效,

我刚刚添加代码来呈现它,

看一看,

    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)

答案 2 :(得分:-1)

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

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