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

IOS 值得注意的地方

2015-07-29 12:50 483 查看
- (void)loadView

{

CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];

UIView *contentView = [[UIView alloc] initWithFrame:applicationFrame];

contentView.backgroundColor = [UIColor blackColor];

self.view = contentView;

levelView = [[LevelView alloc] initWithFrame:applicationFrame viewController:self];

[self.view addSubview:levelView];

}

上述代码首先得到屏幕的frame,然后根据该frame生成了一个contentView,并指定当前ViewController的root view为contentView,然后生成了一个LevelView的自定义View并将它通过addSubview:方法添加到当前ViewController当中,完成view的初始化加载。

关于loadView方法的重写,官方文档中有一个明显的注释,原文如下:

Note: When
overriding the
loadView
method to create your views programmatically,
you should not call
super
. Doing so initiates the default
view-loading behavior and usually just wastes CPU cycles. Your own implementation of the
loadView
method
should do all the work that is needed to create a root view and subviews for your view controller.

意思是当通过代码方式去创建你自己的view时,在loadView方法中不应该调用super,如果调用[super loadView]会影响CPU性能。

2.

1,如果是从代码层面开始使用Autolayout,需要对使用的View的translatesAutoresizingMaskIntoConstraints的属性设置为NO.

即可开始通过代码添加Constraint,否则View还是会按照以往的autoresizingMask进行计算.

而在Interface Builder中勾选了Ues
Autolayout,IB生成的控件的translatesAutoresizingMaskIntoConstraints属性都会被默认设置NO.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: