2016-03-29 189 views
2

我使用的是莢SDCAlertView,我需要一個ImageView的+ A文本框添加到我的內容觀點,但是我有我的約束麻煩,下面是我的代碼。添加約束SDCAlertView

let imageView = UIImageView(frame: CGRectMake(0, 0, 100, 100)) 
imageView.image = UIImage(named: "kalafina") 
imageView.contentMode = .ScaleAspectFill 
let alert = AlertController(title: "Testing", message: "1234") 
imageView.translatesAutoresizingMaskIntoConstraints = false 
alert.contentView.addSubview(imageView) 


let imageHeightConstraint = NSLayoutConstraint(item: imageView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100) 
let imageWidthConstraint = NSLayoutConstraint(item: imageView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100) 
let imageCenterXConstraint = NSLayoutConstraint(item: imageView, attribute: .CenterX, relatedBy: .Equal, toItem: alert.contentView, attribute: .CenterX, multiplier: 1.0, constant: 0.0) 
let imageTopConstraint = NSLayoutConstraint(item: imageView, attribute: .Top, relatedBy: .Equal, toItem: alert.contentView, attribute: .Top, multiplier: 1.0, constant: 0) 
let imageBottomConstraint = NSLayoutConstraint(item: imageView, attribute: .Bottom, relatedBy: .Equal, toItem: alert.contentView, attribute: .Bottom, multiplier: 1.0, constant: 0) 
alert.view.addConstraint(imageTopConstraint) 
alert.view.addConstraint(imageBottomConstraint) 
alert.view.addConstraint(imageHeightConstraint) 
alert.view.addConstraint(imageWidthConstraint) 
alert.view.addConstraint(imageCenterXConstraint) 

alert.addAction(AlertAction(title: "Cancel", style: .Preferred)) 
alert.addAction(AlertAction(title: "Ok", style: .Default, handler: { action in 

    print("Sending") 

})) 
alert.present() 

這是我的結果enter image description here

+0

你期待什麼結果呢? – gabbler

+0

據https://github.com/sberrevoets/SDCAlertView/issues/146,他提到,要警惕大小相應,如果我加入我的約束權 – Happiehappie

回答

1

你加入你的約束alert.view,但你應該把它們添加到alert.contentView。如果你這樣做,一切都按預期工作。

+0

toItem:alert.contentView,肯定我加入到內容查看 – Happiehappie

+0

是的,但然後調用'alert.view.addConstraint()'。這會在準備好加載之前加載視圖。如果您更改爲'alert.contentView.addConstraint()'它應該工作的罰款。 –