2016-03-29 8 views
2

Ich benutze einen der pod SDCAlertView, ich muss ein ImageView + ein textField zu meiner Inhaltsansicht hinzufügen, aber ich habe Probleme mit meinen Einschränkungen, unten ist mein Code.Hinzufügen von Constraints in 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() 

Das ist mein Ergebnis enter image description here

+0

Welches Ergebnis erwarten Sie? – gabbler

+0

Laut https://github.com/sberrevoets/SDCAlertView/issues/146, er erwähnte, dass die Warnung sollte entsprechend Größe, wenn ich meine Einschränkungen richtig hinzufügen – Happiehappie

Antwort

1

Sie fügen Ihre Einschränkungen alert.view, aber man sollte sie zu alert.contentView hinzufügen. Wenn Sie das tun, funktioniert alles wie erwartet.

+0

toItem: alert.contentView, ziemlich sicher, dass ich zu Contentview hinzugefügt – Happiehappie

+0

Ja, aber Rufen Sie dann alert.view.addConstraint() 'auf. Dies lädt die Ansicht, bevor sie geladen werden kann. Wenn Sie das zu 'alert.contentView.addConstraint()' ändern, sollte es gut funktionieren. –