2017-02-17 76 views
1
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)) 

後我添加視圖....關閉按鈕是不工作...如何使這項工作?的iOS警報視圖控制器

回答

0

添加這行代碼

containerView.isUserInteractionEnabled = false 

因爲你containerView覆蓋你的Close Button

+0

好友,和我的關閉按鈕應該在容器下面查看...應該做什麼? –

-1

只需添加一個按鈕,用於關閉警報。
編輯一點點代碼:

alert.addAction(UIAlertAction(title: "Close", style: .UIAlertActionStyle.default, handler: { (action) in 
     // Do some thing 
     print("Do") 
     })) 
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)