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. 运行

iOS: 学习笔记, 用代码驱动自动布局实例的更多相关文章

  1. iOS: 学习笔记, 用代码驱动自动布局实例(swift)

    iOS自动布局是设置iOS界面的利器.本实例展示了如何使用自动布局语言设置水平布局, 垂直布局1. 创建空白iOS项目(swift)2. 添加一个控制器类, 修改YYAppDelegate.swift ...

  2. input子系统学习笔记六 按键驱动实例分析下【转】

    转自:http://blog.chinaunix.net/uid-20776117-id-3212095.html 本文接着input子系统学习笔记五 按键驱动实例分析上接续分析这个按键驱动实例! i ...

  3. python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例

    python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...

  4. iOS学习笔记-精华整理

    iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...

  5. iOS学习笔记总结整理

    来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...

  6. iOS学习笔记之Category

    iOS学习笔记之Category 写在前面 Category是类别(也称为类目或范畴),使用Category,程序员可以为任何已有的类添加方法.使用类别可以对框架提供的类(无法获取源码,不能直接修改) ...

  7. iOS学习笔记之ARC内存管理

    iOS学习笔记之ARC内存管理 写在前面 ARC(Automatic Reference Counting),自动引用计数,是iOS中采用的一种内存管理方式. 指针变量与对象所有权 指针变量暗含了对其 ...

  8. iOS学习笔记06—Category和Extension

    iOS学习笔记06—Category和Extension 一.概述 类别是一种为现有的类添加新方法的方式. 利用Objective-C的动态运行时分配机制,Category提供了一种比继承(inher ...

  9. IOS学习笔记48--一些常见的IOS知识点+面试题

      IOS学习笔记48--一些常见的IOS知识点+面试题   1.堆和栈什么区别? 答:管理方式:对于栈来讲,是由编译器自动管理,无需我们手工控制:对于堆来说,释放工作由程序员控制,容易产生memor ...

随机推荐

  1. 基于OpenCV的人脸识别[iOS开发笔记(2)]

    开始了OpenCV的试水工作了... 1.Get ready 在OpenCV中我们会使用函数cv::CascadeClassifier 来进行人脸检测.但是在使用本函数之前我们需要添加一个XML文件对 ...

  2. Java对象的强、软、弱和虚引用详解

    1.对象的强.软.弱和虚引用 转自:http://zhangjunhd.blog.51cto.com/113473/53092/ 在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无 ...

  3. 在Excel里如何将多个工作簿合并到一个工作簿中

    在Excel里如何将多个工作簿合并到一个工作簿中 当你必须将多个工作簿合并到一个工作簿时,你遇到过麻烦吗?最让人心烦的就是需要合并的工作簿里有很多张工作表.有人能推荐方法解决这个问题吗? 利用VBA ...

  4. WPF button 圆角制作

    将以下节点复制到app.xaml的<Application.Resources>节点下 <Style TargetType="{x:Type Button}"&g ...

  5. Android怎样改动app不在多任务列表中显示

    在实际开发中,我们希望某些activity或者应用程序不在多任务列表中显示,即长按Home键或者多任务button键不显示近期执行的程序,我们能够在对应应用程序的AndroidManifest.xml ...

  6. VWMare CentOS 6.5 静态IP设置

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGVmYXVsdDc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  7. SQL Server与Oracle对比学习:权限管理(二) 一些有趣的比喻

    http://blog.csdn.net/weiwenhp/article/details/8094739 目录(?)[-] SQL Server权限管理 login 与user的区别 角色role ...

  8. XMPP协议实现原理介绍

    本文介绍XMPP协议原理及相关信息. XMPP协议简介   XMPP(Extensible Messageing and Presence Protocol:可扩展消息与存在协议)是目前主流的四种IM ...

  9. [Javascript] MetaProgramming: new.target

    new.target is a new “magical” value available in all functions, thoughin normal functions it will al ...

  10. xcode 4 安装cocos2d-x 2.1.4

    http://blog.csdn.net/xiaominghimi/article/details/6937685 从今天开始Himi将陆续更新cocos2d-X的博文,毕竟cocos2d-X的跨平台 ...