定义限制条件来改变一个 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. 用fcntl()设置堵塞函数的堵塞性质

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types. ...

  2. JS里写入(混写)php asp

    原文:JS里写入(混写)php asp JS里写入(混写)php 方法1:<Br> <script language="javascript"> docum ...

  3. C语言第11课

    主要内容:函数指针 一.函数指针定义 int  maxValue(int  a,int  b) { return   a > b ? a : b; } 函数名和数组名一样是地址,存在在代码区 i ...

  4. STL该反应堆运行

    首先来看全然二叉树的定义: 若设二叉树的深度为h,除第 h 层外,其他各层 (1-h-1) 的结点数都达到最大个数,第 h 层全部的结点都连续集中在最左边,这就是全然二叉树.而将一维数组视为全然二叉树 ...

  5. UITabBarControler解决旋转问题

    在遇到开发项目中播放视频,播放视频回列表后,,列表 NavigationController 里边, NavigationController 在 UItabBarController 里边,不要旋转 ...

  6. sqlserver检测数据库是否能连接的小技巧

    有时候可能需要检测下某台机器的服务是不是起来了,或者某台机器的某个库是不是能被连接又不能打开ssms也不想登陆服务器的话就可以用这个方法. 1.在桌面上右键创建个文本,然后改后缀名为udl以后保存(1 ...

  7. 【 c语言中无符号和有符号的加法运算】【深入理解】--【sky原创】

    原文:[ c语言中无符号和有符号的加法运算][深入理解]--[sky原创]   第一题 #include<stdio.h> int main() { unsigned int a=6; i ...

  8. linux 下一个 jira-6.3.6 组态 皴 翻译 迁移数据库

    每一个版本号翻译包下载  https://translations.atlassian.com/dashboard/download jira下载地址  https://www.atlassian.c ...

  9. Lua 5.2 Reference Manual

    Lua 5.2 Reference Manual.pdf

  10. [C++] 获取IE代理server的账号password

    非常多程序须要使用'浏览器设置'的代理server,IE设置的代理server有可能是须要账号password的.如何编程获取浏览器设置的代理server的账号password呢? InternetQ ...