iOS自動布局

#import<UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (nonatomic, strong) UIButton *leftButton;

@property (nonatomic, strong) UIButton *rightButton;

@property (nonatomic, strong) UITextField *textfield;

@end


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

UIView *superview = self.view;

/*1. Create leftButton and add to our view*/

//創(chuàng)建左邊的按鈕并且添加到視圖上

self.leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

//自動調(diào)整按鈕的約束

self.leftButton.translatesAutoresizingMaskIntoConstraints = NO;

[self.leftButton setTitle:@"LeftButton" forState:UIControlStateNormal];

[self.view addSubview:self.leftButton];

/* 2. Constraint to position LeftButton's X*/

//控制左邊按鈕的 x位置? LayoutConstraint 布局約束

NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint constraintWithItem:self.leftButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute: NSLayoutAttributeCenterX multiplier:1.0 constant:-60.0f];

/* 3. Constraint to position LeftButton's Y*/

//控制左邊按鈕的 y位置? LayoutConstraint 布局約束

NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint constraintWithItem:self.leftButton attribute:NSLayoutAttributeCenterY

relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];

/* 4. Add the constraints to button's superview*/

//添加按鈕的父視圖的限制

[superview addConstraints:@[leftButtonXConstraint,leftButtonYConstraint]];

/*5. Create rightButton and add to our view*/

//創(chuàng)建右邊的按鈕并且添加到視圖上

self.rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

self.rightButton.translatesAutoresizingMaskIntoConstraints = NO;

[self.rightButton setTitle:@"RightButton" forState:UIControlStateNormal];

[self.view addSubview:self.rightButton];

/*6. Constraint to position RightButton's X*/

//控制右邊按鈕的 x位置? LayoutConstraint 布局約束

NSLayoutConstraint *rightButtonXConstraint =[NSLayoutConstraint constraintWithItem:self.rightButton attribute:NSLayoutAttributeCenterX

relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute: NSLayoutAttributeCenterX multiplier:1.0 constant:60.0f];

/*7. Constraint to position RightButton's Y*/

//控制右邊按鈕的 y位置? LayoutConstraint 布局約束

rightButtonXConstraint.priority =UILayoutPriorityDefaultHigh;

NSLayoutConstraint *centerYMyConstraint = [NSLayoutConstraint constraintWithItem:self.rightButton attribute:NSLayoutAttributeCenterY

relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];

[superview addConstraints:@[centerYMyConstraint,rightButtonXConstraint]];

//8. Add Text field

//添加文本框

self.textfield = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, 100, 30)];

self.textfield.borderStyle =1;

self.textfield.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addSubview:self.textfield];

//9. Text field Constraints

//文本字段的約束 頂部

NSLayoutConstraint *textFieldTopConstraint = [NSLayoutConstraint constraintWithItem:self.textfield attribute:NSLayoutAttributeTop

relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:60.0f];

//文本字段的約束 底部

NSLayoutConstraint *textFieldBottomConstraint = [NSLayoutConstraint

constraintWithItem:self.textfield attribute:NSLayoutAttributeTop

relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.rightButton

attribute:NSLayoutAttributeTop multiplier:0.8 constant:-60.0f];

//文本字段的約束 左部

NSLayoutConstraint *textFieldLeftConstraint = [NSLayoutConstraint

constraintWithItem:self.textfield attribute:NSLayoutAttributeLeft

relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:30.0f];

//文本字段的約束 右部

NSLayoutConstraint *textFieldRightConstraint = [NSLayoutConstraint

constraintWithItem:self.textfield attribute:NSLayoutAttributeRight

relatedBy:NSLayoutRelationEqual toItem:superview attribute:

NSLayoutAttributeRight multiplier:1.0 constant:-30.0f];

[superview addConstraints:@[textFieldBottomConstraint ,textFieldLeftConstraint, textFieldRightConstraint,textFieldTopConstraint]];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末鸦泳,一起剝皮案震驚了整個濱河市淹真,隨后出現(xiàn)的幾起案子耸携,更是在濱河造成了極大的恐慌笨忌,老刑警劉巖冬殃,帶你破解...
    沈念sama閱讀 222,729評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件魂角,死亡現(xiàn)場離奇詭異遂填,居然都是意外死亡,警方通過查閱死者的電腦和手機逼蒙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,226評論 3 399
  • 文/潘曉璐 我一進店門从绘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事僵井×藁梗” “怎么了?”我有些...
    開封第一講書人閱讀 169,461評論 0 362
  • 文/不壞的土叔 我叫張陵驹沿,是天一觀的道長艘策。 經(jīng)常有香客問我,道長渊季,這世上最難降的妖魔是什么朋蔫? 我笑而不...
    開封第一講書人閱讀 60,135評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮却汉,結(jié)果婚禮上驯妄,老公的妹妹穿的比我還像新娘。我一直安慰自己合砂,他們只是感情好青扔,可當(dāng)我...
    茶點故事閱讀 69,130評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著翩伪,像睡著了一般微猖。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上缘屹,一...
    開封第一講書人閱讀 52,736評論 1 312
  • 那天凛剥,我揣著相機與錄音,去河邊找鬼轻姿。 笑死犁珠,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的互亮。 我是一名探鬼主播犁享,決...
    沈念sama閱讀 41,179評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼豹休!你這毒婦竟也來了炊昆?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,124評論 0 277
  • 序言:老撾萬榮一對情侶失蹤慕爬,失蹤者是張志新(化名)和其女友劉穎窑眯,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體医窿,經(jīng)...
    沈念sama閱讀 46,657評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,723評論 3 342
  • 正文 我和宋清朗相戀三年炊林,在試婚紗的時候發(fā)現(xiàn)自己被綠了姥卢。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,872評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖独榴,靈堂內(nèi)的尸體忽然破棺而出僧叉,到底是詐尸還是另有隱情,我是刑警寧澤棺榔,帶...
    沈念sama閱讀 36,533評論 5 351
  • 正文 年R本政府宣布瓶堕,位于F島的核電站,受9級特大地震影響症歇,放射性物質(zhì)發(fā)生泄漏郎笆。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,213評論 3 336
  • 文/蒙蒙 一忘晤、第九天 我趴在偏房一處隱蔽的房頂上張望宛蚓。 院中可真熱鬧,春花似錦设塔、人聲如沸凄吏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,700評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽痕钢。三九已至,卻和暖如春序六,著一層夾襖步出監(jiān)牢的瞬間盖喷,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,819評論 1 274
  • 我被黑心中介騙來泰國打工难咕, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留课梳,地道東北人。 一個月前我還...
    沈念sama閱讀 49,304評論 3 379
  • 正文 我出身青樓余佃,卻偏偏與公主長得像暮刃,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子爆土,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,876評論 2 361

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