当它通过自动布局调整有什么方法被调用的的UIView? [英] What method is called on UIView when it's resized by autolayout?

查看:153
本文介绍了当它通过自动布局调整有什么方法被调用的的UIView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我对覆盖在我的子类以下实施圆角的成像图:

I have an image view that I'm implementing rounded corners on by overriding the following in my subclass:

-(void)setFrame:(CGRect)frame {
  [super setFrame:frame];
  [self.layout setCornerRadius:frame.size.width/10.0];
}

这个伟大的工程为图像的初始显示,但如果由于设备旋转或一些其他机制图像的大小变化,这种方法不会被调用来实现调整大小。

This works great for the initial display of the image, but if the size of the image changes due to device rotation or some other mechanism, this method does not get called to implement the resize.

我使用的自动布局,我想知道什么时候我的约束调整视图被称为什么方法的UIView(因而UIImageView的),这样​​我可以重新计算我的角半径每当这个调整大小的发生。我的(显然是假的)的假设是,自动布局系统,称为 SETFRAME:移动/根据需要调整的意见

I'm using autolayout, and I want to know what method of UIView (and thus UIImageView) is being called when my constraints resize the view so that I can recalculate my corner radius whenever this resize takes place. My (apparently false) assumption was that the autolayout system called setFrame: to move / resize views as needed.

推荐答案

从文档 updateConstraints

这是建立约束自己应该这么做通过重写此方法自定义视图

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

...

进行布局之前,您updateConstraints的执行将被调用,让你来验证您的内容的所有必要的约束到位在这个时候你的自定义视图的属性都不会改变。

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:

but if you need to do this on rotational changes, check out willTransitionToTraitCollection:withTransitionCoordinator:

此方法的实现者可以用它来适应基于在newCollection参数的值的接口。这种方法的一个常见用途是进行更改高层presentation风格当电流大小的变化类。

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.

这篇关于当它通过自动布局调整有什么方法被调用的的UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆