iOS: 学习笔记, 用代码驱动自动布局实例
2014-06-07 18:40
465 查看
iOS自动布局是设置iOS界面的利器.
本实例展示了如何使用自动布局语言设置水平布局, 垂直布局
1. 创建空白iOS项目
2. 添加一个控制器类, 修改YYAppDelegate.m文件
3. 修改控制器类
本实例展示了如何使用自动布局语言设置水平布局, 垂直布局
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. } */ @end4. 运行
相关文章推荐
- iOS: 学习笔记, 用代码驱动自动布局实例(swift)
- iOS开发笔记--使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- iOS开发笔记--使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- iOS学习笔记36-Masonry自动布局
- iOS学习笔记 Masonry自动布局
- IOS开发学习笔记038-autolayout 自动布局 界面实现
- iOS学习笔记02——以编码的方式实现Auto Layout自动布局(一)
- IOS布局笔记一(代码实现自动布局)
- iOS学习笔记03——以编码的方式实现Auto Layout自动布局(二)
- iOS开发笔记--使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- iOS学习笔记02——以编码的方式实现Auto Layout自动布局(一)
- linux 驱动学习笔记02--应用实例:在内核中新增驱动代码目录和子目录
- iOS开发笔记--使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- iOS学习笔记02——以编码的方式实现Auto Layout自动布局(一)
- iOS学习笔记36-Masonry自动布局
- IOS学习 autolayout 自动布局 代码与界面设置截图
- iOS学习笔记36-Masonry自动布局
- iOS学习笔记03——以编码的方式实现Auto Layout自动布局(二)
- iOS开发笔记--使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- iOS: 学习笔记实例, 用代码控制视图创建与切换