添加约束时 UIButton 更改框架
UIButton changing frame when constraint added
我在以编程方式创建的 UITableViewCell 中有一个圆形 UIButton。它工作正常,直到我添加一个约束使其在它的超级视图中居中。
这是我的按钮在添加约束之前的样子:
这是我添加约束后的样子:
这是我的代码:
var thumbnailBtn = UIButton.buttonWithType(.Custom) as! UIButton
func setupViews() {
backgroundColor = .colorFromCode(0x3f3f3f)
thumbnailBtn.frame = CGRectMake(0, 0, 80, 80)
thumbnailBtn.layer.cornerRadius = 0.5 * thumbnailBtn.bounds.size.width
thumbnailBtn.backgroundColor = UIColor.greenColor()
thumbnailBtn.setTitle("Test", forState: UIControlState.Normal)
thumbnailBtn.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
contentView.addSubview(menuControlContainerView)
menuControlContainerView.addSubview(thumbnailBtn)
updateConstraints()
}
override func updateConstraints() {
if !didSetupConstraints {
UIView.autoSetPriority(1000) {
self.menuControlContainerView.autoSetContentCompressionResistancePriorityForAxis(.Vertical)
}
menuControlContainerView.autoPinEdgeToSuperviewEdge(.Top, withInset: 0)
menuControlContainerView.autoPinEdgeToSuperviewEdge(.Leading, withInset: 0)
menuControlContainerView.autoPinEdgeToSuperviewEdge(.Trailing, withInset: 0)
menuControlContainerView.autoPinEdgeToSuperviewEdge(.Bottom, withInset: 0)
thumbnailBtn.autoCenterInSuperview()
didSetupConstraints = true
}
super.updateConstraints()
}
我注意到如果我将这行代码 [thumbnailBtn.autoSetDimensionsToSize(CGSizeMake(80, 80))
] 添加到 updateConstraints()
它会起作用并且看起来像这样:
但是这是最干净的方法吗,因为我设置了两次大小?
任何帮助将不胜感激。谢谢。
您在初始帧中设置了大小,但是当您稍后开始使用约束来指定位置(和超级视图布局)时,这是不合适的。实际上,您应该只创建一个普通视图(使用您正在使用的第 3 方库中的适当方法),然后使用约束指定大小和位置(您已经尝试过的是合适的)。
当您将视图居中时,视图很可能会调整大小,因为第 3 方库删除了自动调整大小掩码到约束的转换,这允许约束引擎基本上可以做任何它想做的事情,因为任何设置都满足方程式。
我在以编程方式创建的 UITableViewCell 中有一个圆形 UIButton。它工作正常,直到我添加一个约束使其在它的超级视图中居中。
这是我的按钮在添加约束之前的样子:
这是我添加约束后的样子:
这是我的代码:
var thumbnailBtn = UIButton.buttonWithType(.Custom) as! UIButton
func setupViews() {
backgroundColor = .colorFromCode(0x3f3f3f)
thumbnailBtn.frame = CGRectMake(0, 0, 80, 80)
thumbnailBtn.layer.cornerRadius = 0.5 * thumbnailBtn.bounds.size.width
thumbnailBtn.backgroundColor = UIColor.greenColor()
thumbnailBtn.setTitle("Test", forState: UIControlState.Normal)
thumbnailBtn.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
contentView.addSubview(menuControlContainerView)
menuControlContainerView.addSubview(thumbnailBtn)
updateConstraints()
}
override func updateConstraints() {
if !didSetupConstraints {
UIView.autoSetPriority(1000) {
self.menuControlContainerView.autoSetContentCompressionResistancePriorityForAxis(.Vertical)
}
menuControlContainerView.autoPinEdgeToSuperviewEdge(.Top, withInset: 0)
menuControlContainerView.autoPinEdgeToSuperviewEdge(.Leading, withInset: 0)
menuControlContainerView.autoPinEdgeToSuperviewEdge(.Trailing, withInset: 0)
menuControlContainerView.autoPinEdgeToSuperviewEdge(.Bottom, withInset: 0)
thumbnailBtn.autoCenterInSuperview()
didSetupConstraints = true
}
super.updateConstraints()
}
我注意到如果我将这行代码 [thumbnailBtn.autoSetDimensionsToSize(CGSizeMake(80, 80))
] 添加到 updateConstraints()
它会起作用并且看起来像这样:
但是这是最干净的方法吗,因为我设置了两次大小?
任何帮助将不胜感激。谢谢。
您在初始帧中设置了大小,但是当您稍后开始使用约束来指定位置(和超级视图布局)时,这是不合适的。实际上,您应该只创建一个普通视图(使用您正在使用的第 3 方库中的适当方法),然后使用约束指定大小和位置(您已经尝试过的是合适的)。
当您将视图居中时,视图很可能会调整大小,因为第 3 方库删除了自动调整大小掩码到约束的转换,这允许约束引擎基本上可以做任何它想做的事情,因为任何设置都满足方程式。