我想在手机旋转时更改我的imageView的heightConstraint,我使用下面的代码:
var newPageView = UIImageView(image: nil)
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
let heightConstraint = NSLayoutConstraint(item: newPageView, attribute: .Height, relatedBy: .Equal, toItem: newPageView.superview, attribute: .Height, multiplier: 1, constant: 600)
newPageView.superview!.addConstraint(heightConstraint)
}
但它不起作用。我的代码片段有什么错误?
答案 0 :(得分:0)
imageView
的更改高度,您需要向imageView
添加约束,而不是superview
。我认为最好继续引用约束并在需要时更改它,而不是删除它并添加。例如:
var imageView = UIImageView(image: nil)
var heightConstant : NSLayoutConstraint!
override func viewDidLoad()
{
super.viewDidLoad()
heightConstant = NSLayoutConstraint(item: imageView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 800)
imageView.addConstraint(heightConstant)
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
{
heightConstant.constant = 600
}