iOS: 学习笔记, 用代码驱动自动布局实例

iOS自动布局是设置iOS界面的利器.

本实例展示了如何使用自动布局语言设置水平布局, 垂直布局

1. 创建空白iOS项目

2. 添加一个控制器类, 修改YYAppDelegate.m文件

#import "YYAppDelegate.h"
#import "YYViewController.h"

@implementation YYAppDelegate

- (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 = [[YYViewController alloc] initWithNibName:nil bundle:nil];
    
    [self.window makeKeyAndVisible];
    return YES;
}

3. 修改控制器类

//
//  YYViewController.m
//  UIBasic060701_Button
//
//  Created by yao_yu on 14-6-7.
//  Copyright (c) 2014年 yao_yu. All rights reserved.
//

#import "YYViewController.h"

@interface YYViewController ()

@property(nonatomic, strong) UIView *viewMoveBlock;

@end

@implementation YYViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.viewMoveBlock = [[UIView alloc] init];
    [self.viewMoveBlock setBackgroundColor:[UIColor blueColor]];
    self.viewMoveBlock.frame = CGRectMake(100, 100, 20, 20);
    [self.view addSubview: self.viewMoveBlock];
    
    UIView *commandPane = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 40)];
    [self.view addSubview:commandPane];

    const CGFloat BUTTONSIZE = 40;
    UIButton *buttonLeft = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonLeft setTitle:@"左移" forState:UIControlStateNormal];
    buttonLeft.frame = CGRectMake(0, 0, BUTTONSIZE, BUTTONSIZE);
    [commandPane addSubview: buttonLeft];
    [buttonLeft addTarget:self action:@selector(moveLeft) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *buttonRight = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonRight setTitle:@"右移" forState:UIControlStateNormal];
    buttonRight.frame = CGRectMake(BUTTONSIZE, 0, BUTTONSIZE, BUTTONSIZE);
    [commandPane addSubview: buttonRight];
    [buttonRight addTarget:self action:@selector(moveRight) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *buttonUp = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonUp setTitle:@"上移" forState:UIControlStateNormal];
    buttonUp.frame = CGRectMake(BUTTONSIZE*2, 0, BUTTONSIZE, BUTTONSIZE);
    [commandPane addSubview: buttonUp];
    [buttonUp addTarget:self action:@selector(moveUp) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *buttonDown = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonDown setTitle:@"下移" forState:UIControlStateNormal];
    buttonDown.frame = CGRectMake(BUTTONSIZE*3, 0, BUTTONSIZE, BUTTONSIZE);
    [commandPane addSubview: buttonDown];
    [buttonDown addTarget:self action:@selector(moveDown) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *buttonZoomOut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonZoomOut setTitle:@"放大" forState:UIControlStateNormal];
    buttonZoomOut.frame = CGRectMake(BUTTONSIZE*4, 0, BUTTONSIZE, BUTTONSIZE);
    [commandPane addSubview: buttonZoomOut];
    [buttonZoomOut addTarget:self action:@selector(zoomOut) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *buttonZoomIn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonZoomIn setTitle:@"缩小" forState:UIControlStateNormal];
    buttonZoomIn.frame = CGRectMake(BUTTONSIZE*5, 0, BUTTONSIZE, BUTTONSIZE);
    [commandPane addSubview: buttonZoomIn];
    [buttonZoomIn addTarget:self action:@selector(zoomIn) forControlEvents:UIControlEventTouchUpInside];
    
    [commandPane setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[commandPane(260)]" options:0 metrics:0 views:NSDictionaryOfVariableBindings(commandPane)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[commandPane(50)]" options:0 metrics:0 views:NSDictionaryOfVariableBindings(commandPane)]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:commandPane attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
    
    NSDictionary *metrics = [NSDictionary dictionaryWithObjectsAndKeys:@40, @"SIZE", nil];
    NSDictionary *contraitsView = NSDictionaryOfVariableBindings(buttonLeft, buttonRight, buttonUp, buttonDown, buttonZoomOut, buttonZoomIn);
    for (NSString *buttonKey in contraitsView ) {
        [contraitsView[buttonKey] setTranslatesAutoresizingMaskIntoConstraints:NO];
        [commandPane addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|[%@(SIZE)]", buttonKey] options:0 metrics:metrics views:contraitsView]];
    }
    [commandPane addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[buttonLeft(SIZE)][buttonRight(SIZE)][buttonUp(SIZE)][buttonDown(SIZE)]-(>=0)-[buttonZoomOut(SIZE)][buttonZoomIn(SIZE)]|" options:0 metrics:metrics views: contraitsView]];
}

-(void)moveLeft
{
    [self moveX: -20 Y:0];
}

-(void)moveRight
{
    [self moveX: 20 Y:0];
}

-(void)moveUp
{
    [self moveX: 0 Y:-20];
}

-(void)moveDown
{
    [self moveX: 0 Y:20];
}

-(void)zoomOut
{
    CGRect rect = self.viewMoveBlock.frame;
    
    rect.origin.x -= 20;
    rect.origin.y -= 20;
    rect.size.width += 40;
    rect.size.height += 40;
    
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    
    self.viewMoveBlock.frame = rect;
    
    [UIView commitAnimations];
}

-(void)zoomIn
{
    CGRect rect = self.viewMoveBlock.frame;
    
    rect.origin.x += 20;
    rect.origin.y += 20;
    rect.size.width -= 40;
    rect.size.height -= 40;
    
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    
    self.viewMoveBlock.frame = rect;
    
    [UIView commitAnimations];
}

-(void)moveX: (int)x Y:(int)y
{
    CGPoint p = self.viewMoveBlock.center;
    
    p.x += x;
    p.y += y;
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    
    self.viewMoveBlock.center = p;
    
    [UIView commitAnimations];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
4. 运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值