1、帶xib控制器加載過程
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)btnClicked:(UIButton *)sender {
//如果只執(zhí)行init,它會執(zhí)行initWithNibName:bundle:方法,并且參數(shù)都為空
EOCViewController * vc = [[EOCViewController alloc] init];
//當nibName參數(shù)為nil,表示加載名為EOCViewController的對象,如果bundle為空,表示在主路徑下尋找xib文件
//EOCViewController * vc = [[EOCViewController alloc] initWithNibName:@"EOCViewController" bundle:nil];
[self presentViewController:vc animated:YES completion:^{
}];
}
@end
@implementation EOCViewController
- (instancetype)init{
NSLog(@"%s",__func__);
self = [super init];
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
NSLog(@"%s",__func__);
self = [super initWithCoder:aDecoder];
return self;
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
NSLog(@"%s",__func__);
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
return self;
}
@end
結(jié)果:
-[EOCViewController init]
-[EOCViewController initWithNibName:bundle:]
-[EOCViewController viewDidLoad]
2、帶xib文件,重寫loadView方法。不會加載出來這個xib文件。如果不重寫這個loadView方法,加載xib的時候會自動加載此方法。如:EOCViewController控制器。
- (void)loadView{
NSLog(@"%s",__func__);
[super loadView];
}
3、如果去掉EOCViewController控制器的xib文件,執(zhí)行如下代碼:
- (void)loadView{
NSLog(@"%s",__func__);
// [super loadView];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%s",__func__);
self.view.backgroundColor = [UIColor whiteColor];
}
會造成loadView與viewDidLoad方法循環(huán)調(diào)用。原因是self.view是懶加載方法:
- (UIView *)view{
if (!_view) {
[self loadView];
[self viewDidLoad];
}
return _view;
}
4、在xib中加載視圖控制器,self.view是在viewDidAppear中加載完的(frame為屏幕的大?。?,而不是在viewDidLoad中加載完的(frame為xib的大?。?,所以self.view的frame大小還沒有在viewDidLoad中成型。
不帶xib的控制器被加載,viewDidLoad方法中frame的為手機屏幕的大小。
5、通過重寫loadView改變控制器的視圖.
- (void)loadView{
NSLog(@"%s",__func__);
[super loadView];
self.view = [[NSBundle mainBundle] loadNibNamed:@"EOCView" owner:self options:nil].lastObject;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%s",__func__);
self.view.backgroundColor = [UIColor whiteColor];
}
6、視圖加載過程
EOCView * view = [[NSBundle mainBundle]loadNibNamed:@"EOCView" owner:self options:nil].lastObject;啟動過程:
initWithCoder:
awakeFromNib:
EOCView * view = [[EOCView alloc] init];啟動過程:
init
initWithFrame:
init方法中會封裝initWithFrame:方法。
7、在initWithFrame:方法中添加視圖。
#import "ECOLabel.h"
@implementation ECOLabel
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
view.backgroundColor = [UIColor blueColor];
[self addSubview:view];
}
return self;
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
ECOLabel * lbl = [[ECOLabel alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
lbl.backgroundColor = [UIColor yellowColor];
// lbl.backgroundColor = [UIColor clearColor];
[self.view addSubview:lbl];
}
會出現(xiàn)如下如圖一效果:

ECOLabel對象背景色為黃色 圖一.png
如果將
lbl對象的背景色設置成透明色lbl.backgroundColor = [UIColor clearColor];,如圖二所示:
ECOLabel對象背景色為透明色 圖二.png
原因:在執(zhí)行完initWithFrame:后,視圖才會創(chuàng)建完。打斷點打印self.layer.sublayers的值。
7、
layoutSubviewsframe 變化、scrollView、子類view frame發(fā)生改變和addSubview都會導致layoutSubviews被動執(zhí)行。
8、
drawRectframe發(fā)生變化,drawRect被動執(zhí)行。添加子View,不調(diào)用drawRect方法。因為子View的Frame沒發(fā)生改變。
9、xib可視化
在EOCView類中加入IB_DESIGNABLE 和IBInspectable。
IB_DESIGNABLE
@interface EOCView ()
@property (nonatomic, strong) IBInspectable UIColor * pathColor;
@property (nonatomic, assign) IBInspectable CGFloat pathWith;
@end
- (void)drawRect:(CGRect)rect{
[super drawRect:rect];
CGFloat centerX = (self.bounds.size.width - self.bounds.origin.x) / 2;
CGFloat centerY = (self.bounds.size.height - self.bounds.origin.y) / 2;
UIBezierPath * path = [[UIBezierPath alloc] init];
//添加一個圓形
[path addArcWithCenter:CGPointMake(centerX, centerY) radius:50 startAngle:0 endAngle:360 clockwise:YES];
//設置寬度
path.lineWidth = self.pathWith;
//設置線條顏色
[self.pathColor setStroke];
//繪制線條
[path stroke];
}
效果如圖三所示。

視圖關聯(lián)EOCView對象加入控制器的XIB中 圖三.png