第一種
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
subView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:subView];
第二種
優(yōu)點(diǎn):
1.使代碼邏輯結(jié)構(gòu)層次劃分得更加明顯
2.對(duì)于一個(gè)無(wú)需復(fù)用的小段邏輯也免去了重量級(jí)的調(diào)用函數(shù)
3.當(dāng)你想要復(fù)制粘貼這段代碼時(shí)仙蛉,你僅需要更改一處變量名即可
UIView *subView = ({
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 100, 30)];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
view;
});
subView.backgroundColor = [UIColor yellowColor];