ios – 当UIView通过autolayout调整大小时,会调用什么方法?

前端之家收集整理的这篇文章主要介绍了ios – 当UIView通过autolayout调整大小时,会调用什么方法?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个图像视图,我通过覆盖我的子类中的以下内容来实现圆角:
-(void)setFrame:(CGRect)frame {
  [super setFrame:frame];
  [self.layout setCornerRadius:frame.size.width/10.0];
}

这对于图像的初始显示非常有用,但是如果图像的大小由于设备旋转或某些其他机制而发生变化,则不会调用方法来实现调整大小.

我正在使用autolayout,我想知道当我的约束调整视图大小时调用UIView(以及UIImageView)的方法,以便每当调整大小时我都可以重新计算我的角半径.我的(显然是假的)假设是autolayout系统调用setFrame:根据需要移动/调整视图大小.

解决方法

来自updateConstraints上的文档:

Custom views that set up constraints themselves should do so by overriding this method

….

Before layout is performed,your implementation of updateConstraints will be invoked,allowing you to verify that all necessary constraints for your content are in place at a time when your custom view’s properties are not changing.

或者从layoutSubviews上的文档:

You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.

但如果您需要在轮换时进行此操作,请查看
willTransitionToTraitCollection:withTransitionCoordinator:

Implementors of this method can use it to adapt the interface based on the values in the newCollection parameter. A common use of this method is to make changes to the high-level presentation style when the current size class changes.

原文链接:https://www.f2er.com/iOS/334089.html

猜你在找的iOS相关文章