순수 코드 동적 레이아웃

7716 단어
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

좋은 웹페이지 즐겨찾기