Adding constraints in SDCAlertView

浪尽此生 提交于 2019-12-12 21:06:17

问题


I'm using a the pod SDCAlertView, I need to add an imageView + a textField to my content view, however I am having trouble with my constraints, below is my code.

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

This is my result


回答1:


You are adding your constraints to alert.view, but you should be adding them to alert.contentView. If you do that everything works as expected.



来源:https://stackoverflow.com/questions/36275358/adding-constraints-in-sdcalertview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!