[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有一种渲染的效果,当系统发现屏幕分辨率发生变化了,就会执行这个方法把新的位置重新显示在屏幕上。
- (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有一种渲染的效果,当系统发现屏幕分辨率发生变化了,就会执行这个方法把新的位置重新显示在屏幕上。
相关文章推荐
- iOS字符串 去掉字符串中的空格 回车
- iOS 开发大牛博客
- IOS 真机测试
- iOS开发——判断是否第一次启动
- iOS基础
- NSArray简介
- ios调用系统界面显示英文
- iOS 崩溃日志分析(个人总结,最实用)
- CoreLocation的变化-iOS8
- iOS常用的持久化存储方式
- iOS开发篇(六)Masonry三方框架的使用与介绍
- iOS开发之真机调试
- iOS中修改系统自带的软件上文字描述为中文显示
- 一种iOS7 设置隐藏状态栏的方法
- iOS ANE 真机运行时报错
- IOS算法(三)之插入排序
- ios使用常用正则表达式验证密码身份证手机号
- IOS开发之常见bug
- iOS Document Interaction(预览和打开文档) 编程指南
- iOS单元测试(作用及入门提升)