iOS - UILayoutGuide和NSLayoutAnchor實現(xiàn)自動布局

@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) {
      
      
  }];
        
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末仆葡,一起剝皮案震驚了整個濱河市弦叶,隨后出現(xiàn)的幾起案子憋活,更是在濱河造成了極大的恐慌,老刑警劉巖馋没,帶你破解...
    沈念sama閱讀 222,807評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡巍扛,警方通過查閱死者的電腦和手機肴捉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評論 3 399
  • 文/潘曉璐 我一進(jìn)店門腹侣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人齿穗,你說我怎么就攤上這事傲隶。” “怎么了窃页?”我有些...
    開封第一講書人閱讀 169,589評論 0 363
  • 文/不壞的土叔 我叫張陵跺株,是天一觀的道長复濒。 經(jīng)常有香客問我,道長乒省,這世上最難降的妖魔是什么巧颈? 我笑而不...
    開封第一講書人閱讀 60,188評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮袖扛,結(jié)果婚禮上砸泛,老公的妹妹穿的比我還像新娘。我一直安慰自己蛆封,他們只是感情好唇礁,可當(dāng)我...
    茶點故事閱讀 69,185評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著惨篱,像睡著了一般垒迂。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上妒蛇,一...
    開封第一講書人閱讀 52,785評論 1 314
  • 那天机断,我揣著相機與錄音,去河邊找鬼绣夺。 笑死吏奸,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的陶耍。 我是一名探鬼主播奋蔚,決...
    沈念sama閱讀 41,220評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼烈钞!你這毒婦竟也來了泊碑?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,167評論 0 277
  • 序言:老撾萬榮一對情侶失蹤毯欣,失蹤者是張志新(化名)和其女友劉穎馒过,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體酗钞,經(jīng)...
    沈念sama閱讀 46,698評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡腹忽,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,767評論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了砚作。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片窘奏。...
    茶點故事閱讀 40,912評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖葫录,靈堂內(nèi)的尸體忽然破棺而出着裹,到底是詐尸還是另有隱情,我是刑警寧澤米同,帶...
    沈念sama閱讀 36,572評論 5 351
  • 正文 年R本政府宣布骇扇,位于F島的核電站摔竿,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏匠题。R本人自食惡果不足惜拯坟,卻給世界環(huán)境...
    茶點故事閱讀 42,254評論 3 336
  • 文/蒙蒙 一但金、第九天 我趴在偏房一處隱蔽的房頂上張望韭山。 院中可真熱鬧,春花似錦冷溃、人聲如沸钱磅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽盖淡。三九已至,卻和暖如春凿歼,著一層夾襖步出監(jiān)牢的瞬間褪迟,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評論 1 274
  • 我被黑心中介騙來泰國打工答憔, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留味赃,地道東北人。 一個月前我還...
    沈念sama閱讀 49,359評論 3 379
  • 正文 我出身青樓虐拓,卻偏偏與公主長得像心俗,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子蓉驹,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,922評論 2 361

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