IOS 值得注意的地方

来源:互联网 发布:java接口与安卓对接 编辑:程序博客网 时间:2024/05/02 02:02
  1. - (void)loadView  
  2. {  
  3.     CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];  
  4.     UIView *contentView = [[UIView alloc] initWithFrame:applicationFrame];  
  5.     contentView.backgroundColor = [UIColor blackColor];  
  6.     self.view = contentView;  
  7.    
  8.     levelView = [[LevelView alloc] initWithFrame:applicationFrame viewController:self];  
  9.     [self.view addSubview:levelView];  
  10. }  
上述代码首先得到屏幕的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,需要对使用的ViewtranslatesAutoresizingMaskIntoConstraints的属性设置为NO.
即可开始通过代码添加Constraint,否则View还是会按照以往的autoresizingMask进行计算.
而在Interface Builder中勾选了Ues Autolayout,IB生成的控件的translatesAutoresizingMaskIntoConstraints属性都会被默认设置NO.

0 0
原创粉丝点击