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. [课堂实践与项目]手机QQ客户端--4期(SQLite的加入,注册,找回,登录界面的修改):建立关于QQ注册类,使用SQLite进行存储,

    经过昨天下午和今天上午的不懈努力,终于通过了SQLite的学习. 我们现在这里定义一个有关SQLIte的封装类,便于我在后面的用户注册,用户密码找回,和登录界面的使用 1.首先我们看看我们建立的use ...

  2. NIO组件Selector工作机制详解(下)

    转自:http://blog.csdn.net/haoel/article/details/2224069 五.  迷惑不解 : 为什么要自己消耗资源? 令人不解的是为什么我们的Java的New I/ ...

  3. notepad++下载Subversion插件,显示intalltion of subversion failed

    notepad++安卓subversion的插件不成功,是因为我们下载TortoiseSVN客户端的版本跟subversion的版本不兼容 一.背景: 在新浪云平台上开发微信公众账号,因为要使用SVN ...

  4. 3第一周课后练习·阅读计划(2)-使用指针来访问私有数据成员

    /* * Copyright (c) 2015, 计算机科学学院,烟台大学 * All rights reserved. * 文件名:test.cpp * 作 靠:刘畅 * 完成日期:2015年 3 ...

  5. UIImage载入图片的几种方式及差别

    用UIImage载入图像的方法非常多.最经常使用的是几种:  1.使用imageNamed函数载入: <span style="font-size:14px;">[UI ...

  6. LabVIEW设计模式系列——资源关闭后错误处理

    标准: 1.很多引用资源其打开函数和关闭函数对错误处理的方式有所不同:2.一般地NI的Help里对打开函数的错误端子的解释是这样的:如错误发生在VI或函数运行之前,VI或函数将把错误输入值传递至错误输 ...

  7. Lua的安装

      Lua 是一个扩展式程序设计语言,它被设计成支持通用的过程式编程,并有相关数据描述的设施. Lua 也能对面向对象编程,函数式编程,数据驱动式编程提供很好的支持.它可以作为一个强大.轻量的脚本语言 ...

  8. android注解使用详解(图文)

    在使用Java的SSH框架的时候,一直在感叹注解真是方便啊,关于注解的原理,大家可以参考我的另一片文章Java注解详解.最近有时间研究了android注解的使用,今天与大家分享一下. android中 ...

  9. WSDL阅读方法

    我们以天气预报WebService服务为例,来看看怎么阅读一个wsdl文档. 打开一个wsdl文档后,先看底部. binding在这里: portType在这里: 好了,看了上面的,我们来说说wsdl ...

  10. Linux下搭建Oracle11g RAC(8)----创建ASM磁盘组

    以grid用户创建ASM磁盘组,创建的ASM磁盘组为下一步创建数据库提供存储. ① grid用户登录图形界面,执行asmca命令来创建磁盘组: ② 进入ASMCA配置界面后,单击Create,创建新的 ...