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. 运行
iOS: 学习笔记, 用代码驱动自动布局实例的更多相关文章
- iOS: 学习笔记, 用代码驱动自动布局实例(swift)
iOS自动布局是设置iOS界面的利器.本实例展示了如何使用自动布局语言设置水平布局, 垂直布局1. 创建空白iOS项目(swift)2. 添加一个控制器类, 修改YYAppDelegate.swift ...
- input子系统学习笔记六 按键驱动实例分析下【转】
转自:http://blog.chinaunix.net/uid-20776117-id-3212095.html 本文接着input子系统学习笔记五 按键驱动实例分析上接续分析这个按键驱动实例! i ...
- python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例
python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...
- iOS学习笔记-精华整理
iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...
- iOS学习笔记总结整理
来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...
- iOS学习笔记之Category
iOS学习笔记之Category 写在前面 Category是类别(也称为类目或范畴),使用Category,程序员可以为任何已有的类添加方法.使用类别可以对框架提供的类(无法获取源码,不能直接修改) ...
- iOS学习笔记之ARC内存管理
iOS学习笔记之ARC内存管理 写在前面 ARC(Automatic Reference Counting),自动引用计数,是iOS中采用的一种内存管理方式. 指针变量与对象所有权 指针变量暗含了对其 ...
- iOS学习笔记06—Category和Extension
iOS学习笔记06—Category和Extension 一.概述 类别是一种为现有的类添加新方法的方式. 利用Objective-C的动态运行时分配机制,Category提供了一种比继承(inher ...
- IOS学习笔记48--一些常见的IOS知识点+面试题
IOS学习笔记48--一些常见的IOS知识点+面试题 1.堆和栈什么区别? 答:管理方式:对于栈来讲,是由编译器自动管理,无需我们手工控制:对于堆来说,释放工作由程序员控制,容易产生memor ...
随机推荐
- Android——C语言、JNI与低层调用
JNI java native interface c的基本数据类型 int:32位,能表示的数字是2的32次方个 最高位用来表示符号位,那么还剩下31位可以表示数值,所以能表示的数字就是2的31次方 ...
- 高性能MySql进化论(十一):常见查询语句的优化
总结一下常见查询语句的优化方式 1 COUNT 1. COUNT的作用 · COUNT(table.filed)统计的该字段非空值的记录行数 · ...
- svn 上传 过滤
代码上传过程中发现.so文件不能上传,查了一下,发现是svn服务器要设置上传过滤:很多文件,会被过滤掉,不能正常上传.设置如下: 通过终端打开配置文件: open ~/.subversion/conf ...
- console.debug()浏览器控制台打印输出 仅仅在支持console的浏览器下打印
console.debug()浏览器控制台打印输出 仅仅在支持console的浏览器下打印 var util = {}; /** * 工具类 */ util = new function() { /* ...
- sort()排序 collections.sort();
1.main方法: public class Test { public static void main(String[] args) { /** * * sort()方法详解 * 1.Collec ...
- PureMVC(JS版)源码解析(六):MacroCommand类
上一篇博客,我们讲解了SimpleCommand类,接下来我们看一下与SimpleCommand类很相似的MacroCommand类. MacroCommand类和SimpleCommand类一样,都 ...
- myeclipse2014新感悟
部署有两种方式:1.直接把文件拷贝到 tomcat下的webroot文件夹下 2.myeclipse软件内部点击“deploy”部署 →点击add→tomcat下的webroot文件夹下 点击完“运行 ...
- Android(java)学习笔记189:eclipse 导入项目是提示:某些项目因位于工作空间目录中而被隐藏。
导致这个错误的原因是工程重名了: 并不是仅仅指文件夹重名,相信很多人也曾经修改过文件夹的名称,可惜没什么用处,关键是修改工程里面的一个文件!也就是.project这个文件! 用记事本打开,修改一下&l ...
- Eclipse将项目部署tomcat的webapps目录
Eclipse将项目部署tomcat的webapps目录 >>>>>>>>>>>>>>>>>& ...
- DEDE自带的采集功能,标题太短的解决方法
DEDE自带的采集功能,标题太短获取不全的解决方法搜索了好多之后,终于还是在dede的官方论坛找到了答案.增加采集标题长度:255 目的:原标题长度太小,影响文件采集⑴修改系统参数设置(文档标题最大 ...