本文介绍了在SDCAlertView中添加约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Pod SDCAlertView,我需要在内容视图中添加一个imageView +一个textField,但是我在约束方面遇到了麻烦,下面是我的代码.
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()
这是我的结果
推荐答案
您正在将约束添加到alert.view
,但是您应该将它们添加到alert.contentView
.如果这样做,一切都会按预期进行.
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.
这篇关于在SDCAlertView中添加约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!