简体   繁体   中英

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

After i add the view ....close button is not working...how to make this work?

add this line of code

containerView.isUserInteractionEnabled = false

because your containerView covered your Close Button

I had tried your code and it works completely,

I had just add code to present it,

Take a look on it,

    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)

Just add one more button for closing alert.
Edit a little bit code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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