繁体   English   中英

动态更改autolayout控制的视图的宽高比

[英]Dynamically change aspect ratio of view controlled by autolayout

问题

在UI中,我有一些视图,哪些内容是可伸缩的,但应该保留一些宽高比。 此宽高比在运行时更改,并且在描述宽高比的某些属性更改时应更新视图布局。

我已经公开了NSLayoutConstraint ,但其属性用于宽高比: multiplier是只读的。 什么是更有趣的类似属性constant可以改变。

可能的解决方案

  1. 我可以使用固定宽度或高度的约束并根据预期比率计算它,但这需要额外的锅炉板代码,这将对几何/布局变化作出反应,我宁愿避免这种情况。
  2. 我可以尝试使用intrisicContentSizesizeThatFits:但我不确定这应该如何与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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM