手動(dòng)和自動(dòng)布局子視圖

1. 創(chuàng)建一個(gè)父視圖和多個(gè)子視圖

新建一個(gè)類作儿,叫做SuperView闪檬,作為父親視圖類低零。


在SuperView.h中創(chuàng)建子視圖對(duì)象:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}

#import <UIKit/UIKit.h>

@interface SuperView : UIView
{
    UIView* _view01;
    UIView* _view02;
    UIView* _view03;
    UIView* _view04;
    UIView* _view05;
}

-(void)createSubViews;

@end

在SuperView.m中實(shí)現(xiàn)創(chuàng)建子視圖方法:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s3 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s4 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s5 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s6 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s7 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s8 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s9 {font-variant-ligatures: no-common-ligatures; color: #272ad8}

#import "SuperView.h"

@implementation SuperView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(void)createSubViews
{
    //左上角視圖
    _view01=[[UIView alloc]init];
    _view01.frame=CGRectMake(0, 0, 40, 40);
    
    
    //右上角視圖
    _view02=[[UIView alloc]init];
    _view02.frame=CGRectMake(self.bounds.size.width-40, 0, 40, 40);

    //左下角視圖
    _view03=[[UIView alloc]init];
    _view03.frame=CGRectMake(0, self.bounds.size.height-40, 40, 40);
    
    
    //右下角視圖
    _view04=[[UIView alloc]init];
    _view04.frame=CGRectMake(self.bounds.size.width-40, self.bounds.size.height-40, 40, 40);
    
    
    _view01.backgroundColor=[UIColor orangeColor];
    _view02.backgroundColor=[UIColor orangeColor];
    _view03.backgroundColor=[UIColor orangeColor];
    _view04.backgroundColor=[UIColor orangeColor];
    
    [self addSubview:_view01];
    [self addSubview:_view02];
    [self addSubview:_view03];
    [self addSubview:_view04];
    
}

@end

之后在ViewController.m中創(chuàng)建父親視圖對(duì)象:
(注意:import SuperView.h)

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #31595d}p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s7 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s8 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s9 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s10 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s11 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}

#import "ViewController.h"
#import "SuperView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    
    
    
    
    // Do any additional setup after loading the view, typically from a nib.
    SuperView* sView=[[SuperView alloc]init];
    sView.frame=CGRectMake(20,20 , 180, 280);
    sView.backgroundColor=[UIColor blueColor];
    [self.view addSubview:sView];
    
    
    
    UIButton* btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame=CGRectMake(240, 480, 80, 40);
    [btn setTitle:@"放大" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(pressLarge) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    
    
    UIButton* btn02=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn02.frame=CGRectMake(240, 520, 80, 40);
    [btn02 setTitle:@"縮小" forState:UIControlStateNormal];
    [btn02 addTarget:self action:@selector(pressSmall) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn02];
    
    sView.tag=101;
    
    [sView createSubViews];
    
}

//放大父親視圖
-(void)pressLarge
{
    SuperView* sView=(SuperView*)[self.view viewWithTag:101];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    sView.frame=CGRectMake(20, 20, 300, 400);
    [UIView commitAnimations];
}

//縮小父親視圖
-(void)pressSmall
{
    SuperView* sView=(SuperView*)[self.view viewWithTag:101];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    sView.frame=CGRectMake(20, 20, 180, 280);
    [UIView commitAnimations];
}

運(yùn)行結(jié)果為:

屏幕快照 2016-11-22 下午10.50.33.png
屏幕快照 2016-11-22 下午10.50.38.png

可以看到子視圖沒有隨著父親視圖放大而放大

2. 使子視圖隨父視圖變化

在SuperView.m中添加:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s6 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s7 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s8 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}

//當(dāng)需要重新布局時(shí)
-(void)layoutSubviews
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    
    _view01.frame=CGRectMake(0, 0, 40, 40);
    _view02.frame=CGRectMake(self.bounds.size.width-40, 0, 40, 40);
    _view03.frame=CGRectMake(0, self.bounds.size.height-40, 40, 40);
    _view04.frame=CGRectMake(self.bounds.size.width-40, self.bounds.size.height-40, 40, 40);
    
    [UIView commitAnimations];
}

當(dāng)需要調(diào)整布局時(shí)就調(diào)用layoutSubviews函數(shù)

運(yùn)行結(jié)果為:

屏幕快照 2016-11-22 下午10.54.37.png

3. 自動(dòng)布局

很簡(jiǎn)單橱脸,只需要用到UI控件的點(diǎn)方法:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81}span.s1 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s2 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s3 {font-variant-ligatures: no-common-ligatures}

XXX.autoresizingMask=UIViewAutoresizing.....
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末抒和,一起剝皮案震驚了整個(gè)濱河市膳沽,隨后出現(xiàn)的幾起案子汗菜,更是在濱河造成了極大的恐慌让禀,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,427評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件陨界,死亡現(xiàn)場(chǎng)離奇詭異巡揍,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)菌瘪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門腮敌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人俏扩,你說我怎么就攤上這事糜工。” “怎么了动猬?”我有些...
    開封第一講書人閱讀 165,747評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵啤斗,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我赁咙,道長(zhǎng)钮莲,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,939評(píng)論 1 295
  • 正文 為了忘掉前任彼水,我火速辦了婚禮崔拥,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘凤覆。我一直安慰自己链瓦,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評(píng)論 6 392
  • 文/花漫 我一把揭開白布盯桦。 她就那樣靜靜地躺著慈俯,像睡著了一般。 火紅的嫁衣襯著肌膚如雪拥峦。 梳的紋絲不亂的頭發(fā)上贴膘,一...
    開封第一講書人閱讀 51,737評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音略号,去河邊找鬼刑峡。 笑死,一個(gè)胖子當(dāng)著我的面吹牛玄柠,可吹牛的內(nèi)容都是我干的突梦。 我是一名探鬼主播,決...
    沈念sama閱讀 40,448評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼羽利,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼宫患!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起铐伴,我...
    開封第一講書人閱讀 39,352評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤撮奏,失蹤者是張志新(化名)和其女友劉穎俏讹,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體畜吊,經(jīng)...
    沈念sama閱讀 45,834評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡泽疆,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評(píng)論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了玲献。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片殉疼。...
    茶點(diǎn)故事閱讀 40,133評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖捌年,靈堂內(nèi)的尸體忽然破棺而出瓢娜,到底是詐尸還是另有隱情,我是刑警寧澤礼预,帶...
    沈念sama閱讀 35,815評(píng)論 5 346
  • 正文 年R本政府宣布眠砾,位于F島的核電站,受9級(jí)特大地震影響托酸,放射性物質(zhì)發(fā)生泄漏褒颈。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評(píng)論 3 331
  • 文/蒙蒙 一励堡、第九天 我趴在偏房一處隱蔽的房頂上張望谷丸。 院中可真熱鬧,春花似錦应结、人聲如沸刨疼。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽揩慕。三九已至,卻和暖如春扮休,著一層夾襖步出監(jiān)牢的瞬間漩绵,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工肛炮, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,398評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像吐限,于是被迫代替她去往敵國(guó)和親霹肝。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容