[英]Dynamically change aspect ratio of view controlled by autolayout
In UI I have some view which content is stretchable but should conserve some aspect ratio. 在UI中,我有一些视图,哪些内容是可伸缩的,但应该保留一些宽高比。 This aspect ratio changes during run-time and views layout should be updated when some property describing aspect ratio changes.
此宽高比在运行时更改,并且在描述宽高比的某些属性更改时应更新视图布局。
I've exposed NSLayoutConstraint
, but its property used for aspect ratio: multiplier
is read only. 我已经公开了
NSLayoutConstraint
,但其属性用于宽高比: multiplier
是只读的。 What is more funny similar property constant
can be changed. 什么是更有趣的类似属性
constant
可以改变。
intrisicContentSize
and sizeThatFits:
but I'm not sure how this should be used with NSLayoutConstraint
intrisicContentSize
和sizeThatFits:
但我不确定这应该如何与NSLayoutConstraint
一起使用 Ok I'm whining to much. 好吧,我抱怨很多。 Removing and recreating constraint is not such a big problem (thanks @vacawama):
删除和重新创建约束不是一个大问题(感谢@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.