純代碼動態(tài)布局

iOS6 CN P223
AppDelegate.m 文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = [[ViewController alloc] init];
    [self.window makeKeyAndVisible];
    return YES;
}

ViewController.m 文件

#import "ViewController.h"

@interface ViewController ()

@end

@interface UIWindow (AutoLayoutDebug)
+ (UIWindow *)keyWindow;
- (NSString *)_autolayoutTrace;
@end

@implementation ViewController
{
    UIButton *leftButton;
    UIButton *centerButton;
    UIButton *rightButton;
    BOOL buttonIsVisible;
    NSLayoutConstraint *centerYConstraint;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSLog(@"%@", [[UIWindow keyWindow] _autolayoutTrace]);
}

- (void)didRotateFromInterfaceOrientation:
(UIInterfaceOrientation)fromInterfaceOrientation
{
    [super didRotateFromInterfaceOrientation:
     fromInterfaceOrientation];
    NSLog(@"%@", [[UIWindow keyWindow] _autolayoutTrace]);
}

- (UIButton *)autoLayoutButtonTitle:(NSString *)title backGroundColor:(UIColor *)color
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.translatesAutoresizingMaskIntoConstraints = NO;
    [button setTitle:title forState:UIControlStateNormal];
    button.backgroundColor = color;
    [self.view addSubview:button];
    return button;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    buttonIsVisible = YES;
    //增加三個按鈕
    leftButton = [self autoLayoutButtonTitle:@"Left" backGroundColor:[UIColor redColor]];
    centerButton = [self autoLayoutButtonTitle:@"Center" backGroundColor:[UIColor greenColor]];
    rightButton = [self autoLayoutButtonTitle:@"Right" backGroundColor:[UIColor blueColor]];
    
    [leftButton addTarget:self
                   action:@selector(leftButtonPressed)
         forControlEvents:UIControlEventTouchUpInside];
    [rightButton addTarget:self
                    action:@selector(rightButtonPressed)
          forControlEvents:UIControlEventTouchUpInside];
    
    [self.view setNeedsUpdateConstraints];
}

- (void)rightButtonPressed
{
    if (centerYConstraint.constant == 0.0f)
        centerYConstraint.constant = 100.0f;
    else
        centerYConstraint.constant *= -1.0f;
    [UIView animateWithDuration:0.5f animations:^
    {
        [self.view layoutIfNeeded];
    }];
}

- (void)leftButtonPressed
{
    buttonIsVisible = !buttonIsVisible; // 1
    if (buttonIsVisible) // 2
    {
        [self.view addSubview:centerButton];
        centerButton.alpha = 0.0f;
        
        NSLayoutConstraint *constraint = [NSLayoutConstraint
                      constraintWithItem:centerButton
                      attribute:NSLayoutAttributeCenterX
                      relatedBy:NSLayoutRelationEqual
                      toItem:self.view
                      attribute:NSLayoutAttributeCenterX
                      multiplier:1.0f
                      constant:0.0f];
        [self.view addConstraint:constraint];
        constraint = [NSLayoutConstraint
                      constraintWithItem:centerButton
                      attribute:NSLayoutAttributeCenterY
                      relatedBy:NSLayoutRelationEqual
                      toItem:self.view
                      attribute:NSLayoutAttributeCenterY
                      multiplier:1.0f
                      constant:0.0f];
        [self.view addConstraint:constraint];
        [self.view layoutIfNeeded];
    }
    [self.view setNeedsUpdateConstraints]; // 3
    [UIView animateWithDuration:0.3f animations:^
    {
        [self.view layoutIfNeeded]; // 4
        centerButton.alpha = buttonIsVisible ? 1.0f : 0.0f; // 5
    }
                     completion:^(BOOL finished)
    {
        if (!buttonIsVisible)
            [centerButton removeFromSuperview]; // 6
    }];
}

- (void)updateViewConstraints
{
    NSLog(@"updateViewConstraints");
    [super updateViewConstraints];
    [self.view removeConstraints:self.view.constraints];
    if (buttonIsVisible)
    {
        NSLayoutConstraint *constraint = [NSLayoutConstraint
                      constraintWithItem:centerButton
                      attribute:NSLayoutAttributeCenterX
                      relatedBy:NSLayoutRelationEqual
                      toItem:self.view
                      attribute:NSLayoutAttributeCenterX
                      multiplier:1.0f
                      constant:0.0f];
        [self.view addConstraint:constraint];
        constraint = [NSLayoutConstraint
                      constraintWithItem:centerButton
                      attribute:NSLayoutAttributeCenterY
                      relatedBy:NSLayoutRelationEqual
                      toItem:self.view
                      attribute:NSLayoutAttributeCenterY
                      multiplier:1.0f
                      constant:0.0f];
        centerYConstraint = constraint;
        [self.view addConstraint:constraint];
        
        //[self.view layoutIfNeeded];
        NSDictionary *viewsDictionary =
        NSDictionaryOfVariableBindings(
                                       leftButton, centerButton, rightButton);
        NSArray *constraints = [NSLayoutConstraint
                                constraintsWithVisualFormat:
                                @"[leftButton]-[centerButton]-[rightButton]"
                                options:NSLayoutFormatAlignAllBaseline
                                metrics:nil
                                views:viewsDictionary];
        [self.view addConstraints:constraints];
    }
    else
    {
        NSLayoutConstraint *constraint = [NSLayoutConstraint
                      constraintWithItem:leftButton
                      attribute:NSLayoutAttributeTrailing
                      relatedBy:NSLayoutRelationEqual
                      toItem:self.view
                      attribute:NSLayoutAttributeCenterX
                      multiplier:1.0f
                      constant:-10.0f];
        [self.view addConstraint:constraint];
        constraint = [NSLayoutConstraint
                      constraintWithItem:leftButton
                      attribute:NSLayoutAttributeCenterY
                      relatedBy:NSLayoutRelationEqual
                      toItem:self.view
                      attribute:NSLayoutAttributeCenterY
                      multiplier:1.0f
                      constant:0.0f];
        [self.view addConstraint:constraint];
        constraint = [NSLayoutConstraint
                      constraintWithItem:rightButton
                      attribute:NSLayoutAttributeLeading
                      relatedBy:NSLayoutRelationEqual
                      toItem:self.view
                      attribute:NSLayoutAttributeCenterX
                      multiplier:1.0f
                      constant:10.0f];
        [self.view addConstraint:constraint];
        constraint = [NSLayoutConstraint
                      constraintWithItem:rightButton
                      attribute:NSLayoutAttributeCenterY
                      relatedBy:NSLayoutRelationEqual
                      toItem:self.view
                      attribute:NSLayoutAttributeCenterY
                      multiplier:1.0f
                      constant:0.0f];
        [self.view addConstraint:constraint];
        centerYConstraint = nil;
    }
}
@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末胁澳,一起剝皮案震驚了整個濱河市姿锭,隨后出現(xiàn)的幾起案子麻车,更是在濱河造成了極大的恐慌热押,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件事扭,死亡現(xiàn)場離奇詭異捎稚,居然都是意外死亡,警方通過查閱死者的電腦和手機求橄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進(jìn)店門今野,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人罐农,你說我怎么就攤上這事条霜。” “怎么了涵亏?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵宰睡,是天一觀的道長。 經(jīng)常有香客問我气筋,道長拆内,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任宠默,我火速辦了婚禮麸恍,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘搀矫。我一直安慰自己抹沪,他們只是感情好刻肄,可當(dāng)我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著采够,像睡著了一般肄方。 火紅的嫁衣襯著肌膚如雪冰垄。 梳的紋絲不亂的頭發(fā)上蹬癌,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天,我揣著相機與錄音虹茶,去河邊找鬼逝薪。 笑死,一個胖子當(dāng)著我的面吹牛蝴罪,可吹牛的內(nèi)容都是我干的董济。 我是一名探鬼主播,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼要门,長吁一口氣:“原來是場噩夢啊……” “哼虏肾!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起欢搜,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤封豪,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后炒瘟,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體吹埠,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年疮装,在試婚紗的時候發(fā)現(xiàn)自己被綠了缘琅。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡廓推,死狀恐怖刷袍,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情樊展,我是刑警寧澤做个,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站滚局,受9級特大地震影響居暖,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜藤肢,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一太闺、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧嘁圈,春花似錦省骂、人聲如沸蟀淮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽怠惶。三九已至,卻和暖如春轧粟,著一層夾襖步出監(jiān)牢的瞬間策治,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工兰吟, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留通惫,地道東北人。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓混蔼,卻偏偏與公主長得像履腋,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子惭嚣,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,037評論 2 355

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