[英]Dynamically change aspect ratio of view controlled by autolayout
在UI中,我有一些视图,哪些内容是可伸缩的,但应该保留一些宽高比。 此宽高比在运行时更改,并且在描述宽高比的某些属性更改时应更新视图布局。
我已经公开了NSLayoutConstraint
,但其属性用于宽高比: multiplier
是只读的。 什么是更有趣的类似属性constant
可以改变。
intrisicContentSize
和sizeThatFits:
但我不确定这应该如何与NSLayoutConstraint
一起使用 好吧,我抱怨很多。 删除和重新创建约束不是一个大问题(感谢@vacawama):
- (void)updateASpectRatio: (float)aspectRatio {
// self.aspectRatioConstraint - has to be a strong reference
[self.aspectRatioView removeConstraint: self.aspectRatioConstraint];
self.aspectRatioConstraint = [NSLayoutConstraint constraintWithItem: self.aspectRatioConstraint.firstItem
attribute: self.aspectRatioConstraint.firstAttribute
relatedBy: self.aspectRatioConstraint.relation
toItem: self.aspectRatioConstraint.secondItem
attribute: self.aspectRatioConstraint.secondAttribute
multiplier: self.aspectRatioView.aspectRatio
constant: 0.0];
[self.aspectRatioView addConstraint: self.aspectRatioConstraint];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.