您的位置:首页 > 移动开发 > IOS开发

[iOS]通过xib自定义cell的时候在awakeFromNib方法里面修改view的frame无效

2016-02-18 15:17 513 查看
    通过xib自定义cell,苹果给我们提供了一个awakeFromNib方法:

- (void)awakeFromNib {
// Initialization code
}

通常在这个方法我们做一些cell元素的位置设置什么的。
今天在做cell适配的时候,去掉了cell对应的xib的AutoLayout属性,打算从代码实现.

当我尝试在awakeFromNib修改cell里面的一个button的位置时,发现并没有起作用,

后来在drawRect方法中设置button的frame可以实现我需要的效果。

苹果官方对awakeFromNib方法的说明:

The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized. When an object receives an awakeFromNib message, it is guaranteed to
have all its outlet and action connections already established.

You must call the super implementation of awakeFromNib to give parent classes the opportunity to perform any additional initialization they require. Although the default implementation of this method does nothing, many UIKit classes provide non-empty implementations.
You may call the super implementation at any point during your own awakeFromNib method.

对drawRect方法的说明

The default implementation of this method does nothing. Subclasses that use technologies such as Core Graphics and UIKit to draw their view’s content should override this method and implement their drawing code there. You do not need to override this method
if your view sets its content in other ways. For example, you do not need to override this method if your view just displays a background color or if your view sets its content directly using the underlying layer object.

By the time this method is called, UIKit has configured the drawing environment appropriately for your view and you can simply call whatever drawing methods and functions you need to render your content. Specifically, UIKit creates and configures a graphics
context for drawing and adjusts the transform of that context so that its origin matches the origin of your view’s bounds rectangle. You can get a reference to the graphics context using the UIGraphicsGetCurrentContext function, but do not establish a strong
reference to the graphics context because it can change between calls to the drawRect: method.

我的理解:当自定义cell是从xib创建时,系统会调用awakeFromNib方法,而且这个方法是当所有条件完成,比如建立了outlet,事件等等,是不是也可以假定cell里面的元素位置都初始化完了。而drawRect有一种渲染的效果,当系统发现屏幕分辨率发生变化了,就会执行这个方法把新的位置重新显示在屏幕上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: