2016-03-29 107 views
2

私はポッドSDCAlertViewを使用していますが、コンテンツビューにimageView + textFieldを追加する必要がありますが、私の制約に問題があります。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() 

これは、あなたがalert.viewにあなたの制約を追加しているが、あなたはalert.contentViewに追加する必要がありますenter image description here

+0

どのような結果が期待されますか? – gabbler

+0

https://github.com/sberrevoets/SDCAlertView/issues/146によれば、彼は私の制約を正しく追加すると警告が適切になるはずだと言いました – Happiehappie

答えて

1

私の結果です。そうすれば、すべてが期待通りに機能します。

+0

toItem:alert.contentView、かなり確かにcontentviewに追加しました – Happiehappie

+0

'alert.view.addConstraint()'を呼び出します。これにより、ロード準備が整う前にビューがロードされます。それを 'alert.contentView.addConstraint()'に変更するとうまくいくはずです。 –

関連する問題