iOS: 学习笔记, 用代码驱动自动布局实例
来源:互联网 发布:华硕网络控制器驱动 编辑:程序博客网 时间:2024/06/06 02:04
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(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. 运行
0 0
- iOS: 学习笔记, 用代码驱动自动布局实例
- iOS: 学习笔记, 用代码驱动自动布局实例(swift)
- iOS学习笔记36-Masonry自动布局
- iOS学习笔记36-Masonry自动布局
- iOS学习笔记 Masonry自动布局
- IOS布局笔记一(代码实现自动布局)
- IOS学习 autolayout 自动布局 代码与界面设置截图
- IOS代码实现自动布局
- IOS学习 constraintsWithVisualFormat自动布局
- IOS autolayout自动布局实例(swift)
- 自动布局笔记(学习)
- iOS 圆形布局代码笔记
- ios 代码 自动布局(一)
- ios 代码 自动布局(二)
- 关于iOS代码实现自动布局
- iOS中使用代码自动布局
- ios自动布局使用代码添加约束
- iOS自动布局(代码实现)
- jQuery插件实现新浪微博自动底部加载
- 黑马程序员---dll 程序导出类声明
- Apache 的安装及Apache与php整合过程详解
- HteOS - Win10 Metro UI 风格的Web桌面
- 关于openCV图片裁剪和图片合并的问题
- iOS: 学习笔记, 用代码驱动自动布局实例
- 利用Session实现简单购物
- IOS APP 路径资源
- 一 CocosEditor基础教程第二季 之选道具界面(TableView)
- 软件正交架构
- LInux内核启动
- 48. 搜狗面试题:从尾到头输出链表
- 展讯功能机平台抓log
- MySQL错误:You are using safe update mode and you tried to update a table without a WHERE that uses a K