IOS使用 Visual Format Language 定义水平和垂直约束
1.改变按钮在屏幕上的边距

使用 visual Format Language 在水平方向的限制条件使用的三个例子

//
// VisualFormatLang.h
// AutoLayoutDemo
//
// Created by wildcat on 14-4-21.
// Copyright (c) 2014年 com.wildcat. All rights reserved.
// #import <UIKit/UIKit.h> @interface VisualFormatLang : UIViewController @end //
// VisualFormatLang.m
// AutoLayoutDemo
//
// Created by wildcat on 14-4-21.
// Copyright (c) 2014年 com.wildcat. All rights reserved.
// #import "VisualFormatLang.h" @interface VisualFormatLang ()
//添加私有属性
@property (nonatomic, strong) UITextField *textFieldEmail;
@property (nonatomic, strong) UITextField *textFieldConfirmEmail;
@property (nonatomic, strong) UIButton *registerButton;
@end @implementation VisualFormatLang
//声明全局静态变量
NSString *const kEmailTextFieldHorizontal=@"H:|-[_textFieldEmail]-|";
NSString *const kEmailTextFieldVertical=@"V:|-[_textFieldEmail]"; NSString *const kConfirmEmailHorizontal=@"H:|-[_textFieldConfirmEmail]-|";
NSString *const kConfirmEmailVertical=@"V:[_textFieldEmail]-[_textFieldConfirmEmail]";
NSString *const kRegisterVertical=@"V:[_textFieldConfirmEmail]-[_registerButton]"; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
//构造控件
self.textFieldEmail =[self textFieldWithPlaceholder:@"Email"];
self.textFieldConfirmEmail =[self textFieldWithPlaceholder:@"Confirm Email"];
self.registerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.registerButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.registerButton setTitle:@"Register" forState:UIControlStateNormal];
//添加控件到视图中
[self.view addSubview:self.textFieldEmail];
[self.view addSubview:self.textFieldConfirmEmail];
[self.view addSubview:self.registerButton];
[self.view addConstraints:[self constraints]]; //为父视图添加约束 }
//任意方向旋转
- (NSUInteger) supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll;
}
#pragma mark - 构造UI 组件
//创建了文本框,它包含一个指定的占位符文本,
- (UITextField *) textFieldWithPlaceholder:(NSString *)paramPlaceholder{
UITextField *result = [[UITextField alloc] init];
result.translatesAutoresizingMaskIntoConstraints = NO; //禁止使用autoLayout
result.borderStyle = UITextBorderStyleRoundedRect;
result.placeholder = paramPlaceholder;
return result;
} #pragma mark - 添加约束
- (NSArray *) emailTextFieldConstraints{
NSMutableArray *result = [[NSMutableArray alloc] init];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_textFieldEmail);
[result addObjectsFromArray:
[NSLayoutConstraint
constraintsWithVisualFormat:kEmailTextFieldHorizontal
options:0
metrics:nil
views:viewsDictionary]];
[result addObjectsFromArray:
[NSLayoutConstraint
constraintsWithVisualFormat:kEmailTextFieldVertical
options:0
metrics:nil
views:viewsDictionary]];
return [NSArray arrayWithArray:result];
}
- (NSArray *) confirmEmailTextFieldConstraints{
NSMutableArray *result = [[NSMutableArray alloc] init];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_textFieldConfirmEmail, _textFieldEmail);
[result addObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat:kConfirmEmailHorizontal
options:0
metrics:nil
views:viewsDictionary]];
[result addObjectsFromArray:[NSLayoutConstraint
constraintsWithVisualFormat:kConfirmEmailVertical
options:0
metrics:nil
views:viewsDictionary]];
return [NSArray arrayWithArray:result];
} - (NSArray *) registerButtonConstraints{
NSMutableArray *result = [[NSMutableArray alloc] init];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_registerButton, _textFieldConfirmEmail);
[result addObject:[NSLayoutConstraint constraintWithItem:self.registerButton
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0f
constant:0.0f]];
[result addObjectsFromArray:[NSLayoutConstraint
constraintsWithVisualFormat:kRegisterVertical
options:0
metrics:nil
views:viewsDictionary]];
return [NSArray arrayWithArray:result];
}
//构造并收集所有的限制 条件到一个数组里
- (NSArray *) constraints{
NSMutableArray *result = [[NSMutableArray alloc] init];
[result addObjectsFromArray:[self emailTextFieldConstraints]];
[result addObjectsFromArray:[self confirmEmailTextFieldConstraints]];
[result addObjectsFromArray:[self registerButtonConstraints]];
return [NSArray arrayWithArray:result];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
} @end
接下来学什么,请看:http://1.wildcat.sinaapp.com/?p=68
未完,待续。。。
IOS使用 Visual Format Language 定义水平和垂直约束的更多相关文章
- 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
使用Auto Layout中的VFL(Visual format language)--代码实现自动布局 2014-12-09 10:56 编辑: zhiwupei 分类:iOS开发 来源:机智的新手 ...
- Visual format language
所谓的VFL语言其实就是Visual format language 的缩写,是一种使用代码添加约束的方式,类似于Masonry SDAutolayout的效果,但是操作起来可能要相对简单.一行代码 ...
- iOS开发-VFL(Visual format language)和Autolayout
AutoLayout不管是在StoryBorad还是在xib中都相对来说比较简单,VFL(Visual fromat language)可视化语言基本上用到的比较少,在xCode4时候自动布局的概 ...
- ios 利用size classes 使 iPad 水平和垂直方向布局不同
我们知道ipad全屏幕显示时,无论水平放置还是竖直放置,width 和 height 都是 regular,不像iphone能够区别,那么就不能使用size class 布局不同的水平和垂直界面了吗? ...
- 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局【转】
本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...
- 转载自@机智的新手:使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...
- 【转】使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...
- 转载:使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...
- 使用Auto Layout中的VFL(Visual format language)——代码实现自动布局
本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:api介绍 1.NSLayoutConstraint API NSL ...
随机推荐
- Asp.net Mvc WebSocket
转载一种仿照Asp.net Mvc思维构建WebSocket服务器的方法 问题场景 Asp.net Mvc提供了DependencyResolver.Routing.Filter. Modelbind ...
- Boost Lockfree
Boost Lockfree flyfish 2014-9-30 为了最大限度的挖掘并行编程的性能考虑使用与锁无关的数据结构来编程 与锁无关的数据结构不是依赖于锁和相互排斥来确保线程安全. Lockf ...
- C# 通过ImportNode AppendChild方法合并XmlDocument,XML转为DataTable
var doc1 = new XmlDocument(); var doc2 = new XmlDocument(); XmlNode root1 = doc1.DocumentElement; do ...
- Linux 经常使用 性能 检测 命令 说明
1.uptime [root@smgsim02 ~]# uptime 15:08:15 up 98 days, 4:19, 2 users, load average: 0.07, 0.29, ...
- linux网络编程学习笔记之四 -----多-threaded服务器
对于使用过程中并发.通过实现更轻量级线程. 每个线程都是一个独立的逻辑流. 主题是CPU在执行调度的最小独立单位,这个过程是资源分配单元.当然,这是在微内核操作系统说.总之,这是唯一的一个操作系统内核 ...
- TRS_WCM(拓尔思信息技术有限公司)内容协作平台平台置标经验攻略
TRS_WCM置标过程中经验积累 版本V4.0-2014.6.24-穿越者7号 目录 1.嵌套模板置标 1 2.栏目名称超链接置标 1 3.列表循环输出文档标题包含超链接 1 4.取既定栏目下第一篇文 ...
- IOS被遗忘的知识
IOS ARC项目使用非ARC文件 1.自己的旧项目没有使用ARC,可是引入的第三方库却是使用了ARC的. 对于第一个情况,给採用了ARC的源文件,加入-fobjc-arc选项 2.自己的新项目使用了 ...
- DDD分层架构之聚合
DDD分层架构之聚合 前面已经介绍了DDD分层架构的实体和值对象,本文将介绍聚合以及与其高度相关的并发主题. 我在之前已经说过,初学者第一步需要将业务逻辑尽量放到实体或值对象中,给实体“充血”,这样可 ...
- ASP.NET MVC企业级项目框架
ASP.NET MVC企业级项目框架 MVC项目搭建笔记---- 项目框架采用ASP.NET MVC+Entity Framwork+Spring.Net等技术搭建,搭建过程内容比较多,结合了抽象工厂 ...
- QML Image得到的图片资源路径的详细信息
最近又开始了Qt5.在学习QML当地的资源总是越来越留念类似 " QML Image: Cannot open: qrc:///images/Blue hills.jpg "的错误 ...