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(, , , );
[self.view addSubview: self.viewMoveBlock]; UIView *commandPane = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
[self.view addSubview:commandPane]; const CGFloat BUTTONSIZE = ;
UIButton *buttonLeft = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonLeft setTitle:@"左移" forState:UIControlStateNormal];
buttonLeft.frame = CGRectMake(, , 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, , 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*, , 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*, , 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*, , 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*, , BUTTONSIZE, BUTTONSIZE);
[commandPane addSubview: buttonZoomIn];
[buttonZoomIn addTarget:self action:@selector(zoomIn) forControlEvents:UIControlEventTouchUpInside]; [commandPane setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[commandPane(260)]" options: metrics: views:NSDictionaryOfVariableBindings(commandPane)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[commandPane(50)]" options: metrics: 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:@, @"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: metrics:metrics views:contraitsView]];
}
[commandPane addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[buttonLeft(SIZE)][buttonRight(SIZE)][buttonUp(SIZE)][buttonDown(SIZE)]-(>=0)-[buttonZoomOut(SIZE)][buttonZoomIn(SIZE)]|" options: metrics:metrics views: contraitsView]];
} -(void)moveLeft
{
[self moveX: - Y:];
} -(void)moveRight
{
[self moveX: Y:];
} -(void)moveUp
{
[self moveX: Y:-];
} -(void)moveDown
{
[self moveX: Y:];
} -(void)zoomOut
{
CGRect rect = self.viewMoveBlock.frame; rect.origin.x -= ;
rect.origin.y -= ;
rect.size.width += ;
rect.size.height += ; [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 += ;
rect.origin.y += ;
rect.size.width -= ;
rect.size.height -= ; [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. 运行

上一篇:centos8安装fastdfs6.06集群方式三之:storage的安装/配置/运行


下一篇:iOS: 学习笔记实例, 用代码控制视图创建与切换