@interface ViewController ()
{
NSLayoutConstraint *yellowViewTopConstraint;
NSLayoutConstraint *blueViewLeadConstraint;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UILayoutGuide *view_Guide = self.view.layoutMarginsGuide;
/**
左邊黃色view
*/
UIView *yellow_View = [[UIView alloc]init];
yellow_View.backgroundColor = [UIColor yellowColor];
yellow_View.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:yellow_View];
//左邊距約束
NSLayoutConstraint *yellowView_Leading = [yellow_View.leadingAnchor constraintEqualToAnchor:view_Guide.leadingAnchor constant:10];
//頂部約束
NSLayoutConstraint *yellowView_Top = [yellow_View.topAnchor constraintEqualToAnchor:view_Guide.topAnchor constant:84];
//寬度約束
NSLayoutConstraint *yellowView_Width = [yellow_View.widthAnchor constraintEqualToConstant:100];
//高度約束
NSLayoutConstraint *yellow_Height = [yellow_View.heightAnchor constraintEqualToConstant:100];
[NSLayoutConstraint activateConstraints:@[yellowView_Leading,yellowView_Top,yellow_Height,yellowView_Width]];
yellowViewTopConstraint = yellowView_Top;
/**
居中的紅色view
*/
UIView *middleView = [[UIView alloc]init];
middleView.backgroundColor = [UIColor redColor];
middleView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:middleView];
//水平居中
NSLayoutConstraint *middleView_CenterX = [middleView.centerXAnchor constraintEqualToAnchor:view_Guide.centerXAnchor];
//垂直居中
NSLayoutConstraint *middleView_CenterY = [middleView.centerYAnchor constraintEqualToAnchor:view_Guide.centerYAnchor];
//寬度約束
NSLayoutConstraint *middleView_Width = [middleView.widthAnchor constraintEqualToConstant:100];
//高度約束
NSLayoutConstraint *middleView_Height = [middleView.heightAnchor constraintEqualToConstant:100];
[NSLayoutConstraint activateConstraints:@[middleView_CenterX,middleView_CenterY,middleView_Height,middleView_Width]];
/**
* 創(chuàng)建一個與黃色view相聚10的藍(lán)色view
*/
UIView *blueView = [[UIView alloc]init];
blueView.backgroundColor = [UIColor blueColor];
blueView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:blueView];
//左邊約束
NSLayoutConstraint *blueView_Leading = [blueView.leadingAnchor constraintEqualToAnchor:yellow_View.trailingAnchor constant:10];
//頂部約束于黃色view頂部對齊
NSLayoutConstraint *blueView_Top = [blueView.topAnchor constraintEqualToAnchor:yellow_View.topAnchor];
//寬度約束
NSLayoutConstraint *blueView_Width = [blueView.widthAnchor constraintEqualToConstant:100];
//高度約束
NSLayoutConstraint *blueView_Height = [blueView.heightAnchor constraintEqualToConstant:100];
[NSLayoutConstraint activateConstraints:@[blueView_Leading,blueView_Top,blueView_Width,blueView_Height]];
blueViewLeadConstraint = blueView_Leading;
//創(chuàng)建一個執(zhí)行動畫按鈕
UIButton *btnAnimate = [UIButton buttonWithType:UIButtonTypeCustom];
[btnAnimate setBackgroundColor:[UIColor redColor]];
[btnAnimate setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnAnimate setTitle:@"執(zhí)行動畫" forState:UIControlStateNormal];
[btnAnimate addTarget:self action:@selector(btnAnimateClick:) forControlEvents:UIControlEventTouchUpInside];
btnAnimate.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:btnAnimate];
//水平居中
NSLayoutConstraint *btnAnimate_CenterX = [btnAnimate.centerXAnchor constraintEqualToAnchor:view_Guide.centerXAnchor];
//底部約束
NSLayoutConstraint *btnAnimate_Bottom = [btnAnimate.bottomAnchor constraintEqualToAnchor:view_Guide.bottomAnchor constant:-20];
//寬度約束
NSLayoutConstraint *btnAnimate_Width = [btnAnimate.widthAnchor constraintEqualToConstant:80];
//高度約束
NSLayoutConstraint *btnAnimate_Height = [btnAnimate.heightAnchor constraintEqualToConstant:32];
[NSLayoutConstraint activateConstraints:@[btnAnimate_Height,btnAnimate_Width,btnAnimate_CenterX,btnAnimate_Bottom]];
}
- (void)btnAnimateClick:(UIButton *)sender
{
[UIView animateKeyframesWithDuration:1 delay:0 options:UIViewKeyframeAnimationOptionRepeat animations:^{
yellowViewTopConstraint.constant += 10;
blueViewLeadConstraint.constant += 10;
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
}];
}
iOS - UILayoutGuide和NSLayoutAnchor實現(xiàn)自動布局
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門腹侣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人齿穗,你說我怎么就攤上這事傲隶。” “怎么了窃页?”我有些...
- 文/不壞的土叔 我叫張陵跺株,是天一觀的道長复濒。 經(jīng)常有香客問我,道長乒省,這世上最難降的妖魔是什么巧颈? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮袖扛,結(jié)果婚禮上砸泛,老公的妹妹穿的比我還像新娘。我一直安慰自己蛆封,他們只是感情好唇礁,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著惨篱,像睡著了一般垒迂。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上妒蛇,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼烈钞!你這毒婦竟也來了泊碑?” 一聲冷哼從身側(cè)響起,我...
- 正文 年R本政府宣布骇扇,位于F島的核電站摔竿,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏匠题。R本人自食惡果不足惜拯坟,卻給世界環(huán)境...
- 文/蒙蒙 一但金、第九天 我趴在偏房一處隱蔽的房頂上張望韭山。 院中可真熱鬧,春花似錦冷溃、人聲如沸钱磅。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽盖淡。三九已至,卻和暖如春凿歼,著一層夾襖步出監(jiān)牢的瞬間褪迟,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- UITableView+FDTemplateLayoutCell這個開源類,讓cell高度的自適應(yīng)變得格外容易!具...
- Autolayout簡介 Autolayout是一種“自動布局”技術(shù)城榛,專門用來布局UI界面的, Autolayou...
- 早在前兩年還沒有iPhone6(s)态兴、iPhone6(s) plus出現(xiàn)的時候狠持,在開發(fā)iOS的應(yīng)用程序的時候感覺...
- 重塑自己的研究 以問題為導(dǎo)向 需要列舉很多很多的問題,這些問題列出來瞻润,文章的框架就出來了工坊。第一個問題就是:我這篇文...
- 20多年來,我似乎是憑著自己的“感覺”一直活到了現(xiàn)在敢订。很多人生的選擇都不曾有過事先規(guī)劃王污,很多重大決定也并未考慮太多...