【问题标题】:How to change aspect ratio of a UIView如何更改 UIView 的纵横比
【发布时间】:2015-07-04 19:56:46
【问题描述】:

我有一个自定义 UIView,它以编程方式使用自动布局来设置框架的大小。为此,我将视图的宽度属性设置为与父视图相同的约束,然后将纵横比的约束设置为某个硬编码值

//width constraint
NSLayoutConstraint *widhtConstraint=[NSLayoutConstraint constraintWithItem:entryView
                                                                 attribute:NSLayoutAttributeWidth
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:scrollView
                                                                 attribute:NSLayoutAttributeWidth
                                                                multiplier:1.0f
                                                                  constant:8.0f];

[scrollView addConstraint:widhtConstraint];

//aspect ratio constraint
NSLayoutConstraint *aspectRatioConstraint=[NSLayoutConstraint constraintWithItem:entryView
                                                                       attribute:NSLayoutAttributeWidth
                                                                       relatedBy:NSLayoutRelationEqual
                                                                          toItem:entryView
                                                                       attribute:NSLayoutAttributeHeight
                                                                      multiplier:80.0/27.0//aspect ratio same as formula view
                                                                        constant:0.0f];

[scrollView addConstraint:aspectRatioConstraint];

请参考图片:

我希望通过增加它的高度来改变这个框架的纵横比(查看更多),然后在触摸同一个按钮时将它调整回原来的大小。另外我如何计算总高度由其所有子视图管理的视图,因此每个子视图都可见而无需剪辑。(基本上是标准的折叠功能)

【问题讨论】:

  • 如何操作纵横比约束,使其包含其下的所有子视图?
  • 您能否建议我如何动态更改“aspectRatioConstraint”乘数属性(在这种情况下,它告诉视图的纵横比)。作为 Autolayout 的新手,我在网上也找不到任何东西。

标签: ios uiview autolayout aspect-ratio


【解决方案1】:

您可以在代码中将纵横比作为变量,并让代码在 updateConstraints 等方法中设置约束。

float aspectRatio; NSLayoutConstraint *aspectRatioConstraint=[NSLayoutConstraint constraintWithItem:entryView
                                                                   attribute:NSLayoutAttributeWidth
                                                                   relatedBy:NSLayoutRelationEqual
                                                                      toItem:entryView
                                                                   attribute:NSLayoutAttributeHeight
                                                                  multiplier:self.aspectRatio//aspect ratio same as formula view
                                                                    constant:0.0f];

然后当按钮被按下时,在action方法中你可以修改self.aspectRatio为fit,然后调用setNeedsUpdateConstraints,然后调用setNeedsLayout。

【讨论】:

  • 抱歉,更改原始变量如何动态影响“aspectRatioConstraint”?该变量只是按值传递。 “乘数”也没有设置器。
  • 是的,留给它自己不会动态改变。这就是为什么您需要调用 setNeedsUpdateConstraints 并随后调用 setNeedsLayout。这将导致再次完成约束并布置视图。 stackoverflow.com/a/20927069/1135417
【解决方案2】:

我还没有完全理解问题是什么,但是更改约束以响应按钮按下以展开/折叠超级视图很容易:

如果这是你所追求的,你可以在这里找到一个可下载的示例项目:https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/bk2ch04p183animationAndAutolayout4

【讨论】:

  • 示例建议,更改视图的高度约束,这不是我想要的。我想改变纵横比,使视图变得足够高以封装其中的所有子视图(注意宽度)。
  • 在这个例子中就是这样。子视图决定高度。
猜你喜欢
  • 1970-01-01
  • 2016-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
  • 2013-10-31
  • 2014-10-23
  • 2015-02-28
相关资源
最近更新 更多