定义限制条件来改变一个 UI 组件在其父视图的水平和垂直方向布局的方法。
可以使用方程式里 H:方向符号代表水平方向的边距,使用 V:方向符号代表垂直方向的边 距。
转载请注明,本文转自:http://1.wildcat.sinaapp.com/?p=60
1.改变按钮在屏幕上的边距
 

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

要编写的例子的约束条件如下:
·邮件区域离视图的顶部具有一个标准的垂直方向距离。
·确认邮件的区域在垂直方向山距邮件区域有一个标准的距离。
·注册按钮距确认邮件区域的垂直方向上具有一个标准的距离。
·所有的组件在水平方向上在其父视图中都是居中的。
·邮件和确认邮件区域在水平方向上距父视图两边都有一个标准的距离。
·按钮的宽度被修改成 128 像素。

//
// 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 定义水平和垂直约束的更多相关文章

  1. 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局

    使用Auto Layout中的VFL(Visual format language)--代码实现自动布局 2014-12-09 10:56 编辑: zhiwupei 分类:iOS开发 来源:机智的新手 ...

  2. Visual format language

    所谓的VFL语言其实就是Visual format language 的缩写,是一种使用代码添加约束的方式,类似于Masonry  SDAutolayout的效果,但是操作起来可能要相对简单.一行代码 ...

  3. iOS开发-VFL(Visual format language)和Autolayout

    AutoLayout不管是在StoryBorad还是在xib中都相对来说比较简单,VFL(Visual  fromat  language)可视化语言基本上用到的比较少,在xCode4时候自动布局的概 ...

  4. ios 利用size classes 使 iPad  水平和垂直方向布局不同

    我们知道ipad全屏幕显示时,无论水平放置还是竖直放置,width 和 height 都是 regular,不像iphone能够区别,那么就不能使用size class 布局不同的水平和垂直界面了吗? ...

  5. 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局【转】

    本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...

  6. 转载自@机智的新手:使用Auto Layout中的VFL(Visual format language)--代码实现自动布局

    本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...

  7. 【转】使用Auto Layout中的VFL(Visual format language)--代码实现自动布局

    本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...

  8. 转载:使用Auto Layout中的VFL(Visual format language)--代码实现自动布局

    本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...

  9. 使用Auto Layout中的VFL(Visual format language)——代码实现自动布局

    本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:api介绍 1.NSLayoutConstraint API NSL ...

随机推荐

  1. 使用Navicat Premium 和PL\SQL Developer连接Oracl

    在64位Win7中使用Navicat Premium 和PL\SQL Developer连接Oracle数据库备忘   最近接手了一个项目,服务器端数据库是oracle 11g 64位.由于主要工作不 ...

  2. 队列--Redis+Log4Net

    队列--Redis+Log4Net Redis简介 Redis是一个开源的,使用C语言编写,面向“键/值”对类型数据的分布式NoSQL数据库系统,特点是高性能,持久存储,适应高并发的应用场景.Redi ...

  3. c#数据库操作大全

    原文:c#数据库操作大全 1.提取单条记录 //using System.Data; //using System.Data.SqlClient;  using (SqlConnection cn = ...

  4. C#中ISpostback

    响应客户端控件时ispostback为true 代码: using System; using System.Collections.Generic; using System.Linq; using ...

  5. hust1384---The value of F[n]

    Description For any integer i>=3 we have F[i]=(F[i-1]+2*F[i-2]+3*F[i-3])%9901; Now give you F[0], ...

  6. APMServ—我用过的最优秀的PHP集成环境工具

    原文:APMServ-我用过的最优秀的PHP集成环境工具 经常折腾wordpress和各种cms,免不了要在本地测试一些程序,所以选择一款好的php集成环境就至关重要啦. 1. 我用过的php集成环境 ...

  7. EasyUI基础知识Draggable(拖累)

    学习前easyui基于解析器,装载机.对他们来说,入门阶段,我们只需要在一个简单的理解.第一阶段,不宜过深后,.接着,根据easyui订购的文件正在研究安排官方网站Draggable插入. Dragg ...

  8. Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException

    1.错误描写叙述 信息: Initializing c3p0-0.9.2.1 [built 20-March-2013 10:47:27 +0000; debug? true; trace: 10] ...

  9. Git基础教程

    Git是一个分布式的版本控制工具,本篇文章从介绍Git开始,重点在于介绍Git的基本命令和使用技巧,让你尝试使用Git的同时,体验到原来一个版 本控制工具可以对开发产生如此之多的影响,文章分为两部分, ...

  10. Linux忘记rootpassword

    我们常常会碰到忘记rootpassword的情况,以下是解决之道,  此方法使用绝大多数的Linux发行版:  1. 首先进入grub  2. 在须要编辑的入口处,按下e,在quite后增加     ...