IOS自动布局XIB和CODE,附代码和PPT
2014-10-21 18:16
316 查看
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(10, 10, 50, 30);
[btnBack setImage:[UIImage imageNamed:@"sp_btn_back"] forState:UIControlStateNormal];
[self.view addSubview:btnBack];
UIButton *btnShare = [UIButton buttonWithType:UIButtonTypeCustom];
btnShare.frame = CGRectMake(270, 10, 50, 30);
[btnShare setImage:[UIImage imageNamed:@"sp_btn_share"] forState:UIControlStateNormal];
[self.view addSubview:btnShare];
UIButton *btnDetail = [UIButton buttonWithType:UIButtonTypeCustom];
btnDetail.frame = CGRectMake(10, 380, 50, 30);
[btnDetail setImage:[UIImage imageNamed:@"night_search_category_news"] forState:UIControlStateNormal];
[self.view addSubview:btnDetail];
UIButton *btnLikes = [UIButton buttonWithType:UIButtonTypeCustom];
btnLikes.frame = CGRectMake(270, 380, 50, 30);
[btnLikes setImage:[UIImage imageNamed:@"sp_btn_share"] forState:UIControlStateNormal];
[self.view addSubview:btnLikes];
[btnBack setTranslatesAutoresizingMaskIntoConstraints:NO];
[btnShare setTranslatesAutoresizingMaskIntoConstraints:NO];
[btnDetail setTranslatesAutoresizingMaskIntoConstraints:NO];
[btnLikes setTranslatesAutoresizingMaskIntoConstraints:NO];
NSDictionary *views = NSDictionaryOfVariableBindings(self.view, btnBack, btnShare, btnDetail, btnLikes);
[self.view addVisualConstraints:@"|-10-[btnBack]" forViews:views];
[self.view addVisualConstraints:@"[btnShare]-10-|" forViews:views];
[self.view addVisualConstraints:@"|-10-[btnDetail]" forViews:views];
[self.view addVisualConstraints:@"V:[btnDetail]-10-|" forViews:views];
[self.view addVisualConstraints:@"[btnLikes]-10-|" forViews:views];
[self.view addVisualConstraints:@"V:[btnLikes]-10-|" forViews:views];
}
复制代码
Uiview扩展代码
#import "UIView+Constraint.h"
@implementation UIView (Constraint)
- (void)addVisualConstraints:(NSString *)constraintString forViews:(NSDictionary *)views {
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraintString
options:0
metrics:0
views:views]];
}
@end
复制代码
Visual Format Syntax
The following are examples of constraints you can specify using the visual format. Note how the text visually matches the image.
Standard Space
[button]-[textField]
Width
Constraint
[button(>=50)]
Connection
to Superview
|-50-[orchidBox]-50-|
Vertical
Layout
V:[topField]-10-[bottomField]
Flush
Views
[maroonView][oceanView]
Priority
[button(100@20)]
Equal
Widths
[button1(==button2)]
Multiple
Predicates
[flexibleButton(>=70,<=100)]
A
Complete Line of Layout
|-[find]-[findNext]-[findField(>=20)]-|
The notation prefers good visualization over completeness of expressibility. There are constraints that cannot be expressed in visual format syntax, although most of the constraints that are useful in real user interfaces can be. One useful constraint that
cannot be expressed is a fixed aspect ratio (for example,imageView.width = 2 * imageView.height). To create such a constraint, you must useconstraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:.
Visual Format String Grammar
The visual format string grammar is defined as follows (literals are shown in code font; e denotes
the empty string).
[align=left]Symbol[/align]
[align=left]Replacement rule[/align]
[align=left]<visualFormatString>[/align]
[align=left](<orientation>:)?[/align]
[align=left](<superview><connection>)?[/align]
[align=left]<view>(<connection><view>)*[/align]
[align=left](<connection><superview>)?[/align]
[align=left]<orientation>[/align]
[align=left]H|V[/align]
[align=left]<superview>[/align]
[align=left]|[/align]
[align=left]<view>[/align]
[align=left][<viewName>(<predicateListWithParens>)?][/align]
[align=left]<connection>[/align]
[align=left]e|-<predicateList>-|-[/align]
[align=left]<predicateList>[/align]
[align=left]<simplePredicate>|<predicateListWithParens>[/align]
[align=left]<simplePredicate>[/align]
[align=left]<metricName>|<positiveNumber>[/align]
[align=left]<predicateListWithParens>[/align]
[align=left](<predicate>(,<predicate>)*)[/align]
[align=left]<predicate>[/align]
[align=left](<relation>)?(<objectOfPredicate>)(@<priority>)?[/align]
[align=left]<relation>[/align]
[align=left]==|<=|>=[/align]
[align=left]<objectOfPredicate>[/align]
[align=left]<constant>|<viewName> (see note)[/align]
[align=left]<priority>[/align]
[align=left]<metricName>|<number>[/align]
[align=left]<constant>[/align]
[align=left]<metricName>|<number>[/align]
[align=left]<viewName>[/align]
[align=left]Parsed as a C identifier.[/align]
[align=left]This must be a key mapping to an instance of NSView in the passed views dictionary.[/align]
[align=left]<metricName>[/align]
[align=left]Parsed as a C identifier. This must be a key mapping to an instance of NSNumber in the passed metrics dictionary.[/align]
[align=left]<number>[/align]
[align=left]As parsed by strtod_l, with the C locale.[/align]
Note: For the objectOfPredicate production, a viewName is only acceptable if the subject of the predicate is the width or height of a view. That is, you can use[view1(==view2)]
to specify that view1 and view2 have the same width.
{
[super viewDidLoad];
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(10, 10, 50, 30);
[btnBack setImage:[UIImage imageNamed:@"sp_btn_back"] forState:UIControlStateNormal];
[self.view addSubview:btnBack];
UIButton *btnShare = [UIButton buttonWithType:UIButtonTypeCustom];
btnShare.frame = CGRectMake(270, 10, 50, 30);
[btnShare setImage:[UIImage imageNamed:@"sp_btn_share"] forState:UIControlStateNormal];
[self.view addSubview:btnShare];
UIButton *btnDetail = [UIButton buttonWithType:UIButtonTypeCustom];
btnDetail.frame = CGRectMake(10, 380, 50, 30);
[btnDetail setImage:[UIImage imageNamed:@"night_search_category_news"] forState:UIControlStateNormal];
[self.view addSubview:btnDetail];
UIButton *btnLikes = [UIButton buttonWithType:UIButtonTypeCustom];
btnLikes.frame = CGRectMake(270, 380, 50, 30);
[btnLikes setImage:[UIImage imageNamed:@"sp_btn_share"] forState:UIControlStateNormal];
[self.view addSubview:btnLikes];
[btnBack setTranslatesAutoresizingMaskIntoConstraints:NO];
[btnShare setTranslatesAutoresizingMaskIntoConstraints:NO];
[btnDetail setTranslatesAutoresizingMaskIntoConstraints:NO];
[btnLikes setTranslatesAutoresizingMaskIntoConstraints:NO];
NSDictionary *views = NSDictionaryOfVariableBindings(self.view, btnBack, btnShare, btnDetail, btnLikes);
[self.view addVisualConstraints:@"|-10-[btnBack]" forViews:views];
[self.view addVisualConstraints:@"[btnShare]-10-|" forViews:views];
[self.view addVisualConstraints:@"|-10-[btnDetail]" forViews:views];
[self.view addVisualConstraints:@"V:[btnDetail]-10-|" forViews:views];
[self.view addVisualConstraints:@"[btnLikes]-10-|" forViews:views];
[self.view addVisualConstraints:@"V:[btnLikes]-10-|" forViews:views];
}
复制代码
Uiview扩展代码
#import "UIView+Constraint.h"
@implementation UIView (Constraint)
- (void)addVisualConstraints:(NSString *)constraintString forViews:(NSDictionary *)views {
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraintString
options:0
metrics:0
views:views]];
}
@end
复制代码
Visual Format Syntax
The following are examples of constraints you can specify using the visual format. Note how the text visually matches the image.
Standard Space
[button]-[textField]
Width
Constraint
[button(>=50)]
Connection
to Superview
|-50-[orchidBox]-50-|
Vertical
Layout
V:[topField]-10-[bottomField]
Flush
Views
[maroonView][oceanView]
Priority
[button(100@20)]
Equal
Widths
[button1(==button2)]
Multiple
Predicates
[flexibleButton(>=70,<=100)]
A
Complete Line of Layout
|-[find]-[findNext]-[findField(>=20)]-|
The notation prefers good visualization over completeness of expressibility. There are constraints that cannot be expressed in visual format syntax, although most of the constraints that are useful in real user interfaces can be. One useful constraint that
cannot be expressed is a fixed aspect ratio (for example,imageView.width = 2 * imageView.height). To create such a constraint, you must useconstraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:.
Visual Format String Grammar
The visual format string grammar is defined as follows (literals are shown in code font; e denotes
the empty string).
[align=left]Symbol[/align]
[align=left]Replacement rule[/align]
[align=left]<visualFormatString>[/align]
[align=left](<orientation>:)?[/align]
[align=left](<superview><connection>)?[/align]
[align=left]<view>(<connection><view>)*[/align]
[align=left](<connection><superview>)?[/align]
[align=left]<orientation>[/align]
[align=left]H|V[/align]
[align=left]<superview>[/align]
[align=left]|[/align]
[align=left]<view>[/align]
[align=left][<viewName>(<predicateListWithParens>)?][/align]
[align=left]<connection>[/align]
[align=left]e|-<predicateList>-|-[/align]
[align=left]<predicateList>[/align]
[align=left]<simplePredicate>|<predicateListWithParens>[/align]
[align=left]<simplePredicate>[/align]
[align=left]<metricName>|<positiveNumber>[/align]
[align=left]<predicateListWithParens>[/align]
[align=left](<predicate>(,<predicate>)*)[/align]
[align=left]<predicate>[/align]
[align=left](<relation>)?(<objectOfPredicate>)(@<priority>)?[/align]
[align=left]<relation>[/align]
[align=left]==|<=|>=[/align]
[align=left]<objectOfPredicate>[/align]
[align=left]<constant>|<viewName> (see note)[/align]
[align=left]<priority>[/align]
[align=left]<metricName>|<number>[/align]
[align=left]<constant>[/align]
[align=left]<metricName>|<number>[/align]
[align=left]<viewName>[/align]
[align=left]Parsed as a C identifier.[/align]
[align=left]This must be a key mapping to an instance of NSView in the passed views dictionary.[/align]
[align=left]<metricName>[/align]
[align=left]Parsed as a C identifier. This must be a key mapping to an instance of NSNumber in the passed metrics dictionary.[/align]
[align=left]<number>[/align]
[align=left]As parsed by strtod_l, with the C locale.[/align]
Note: For the objectOfPredicate production, a viewName is only acceptable if the subject of the predicate is the width or height of a view. That is, you can use[view1(==view2)]
to specify that view1 and view2 have the same width.
相关文章推荐
- IOS开发之Bug--使用xib的自动布局和代码中修改遇到的bug
- [置顶] iOS 兄弟控件的 自动布局实现N等分且宽高比例是1:N(xib 上实现) 代码很简单就不多说
- ios-AutoLayout(自动布局代码控制)简单总结
- ios-AutoLayout(自动布局代码控制)简单总结
- IOS开发-自动布局之autoresizingMask使用详解(Storyboard&Code)
- ios-AutoLayout(自动布局代码控制)简单总结
- iOS xib自动布局时遇到的一些问题
- iOS: 学习笔记, 用代码驱动自动布局实例(swift)
- iOS-- 使用xib实现自动布局
- [IOS]NSLayoutConstraint-代码实现自动布局的函数用法说明
- ios-AutoLayout(自动布局代码控制)简单总结
- iOS-NSLayoutConstraint-代码实现自动布局的函数用法说明
- IOS手动添加的View 在代码中使用(自动布局)autoLayout
- 关于iOS代码实现自动布局
- IOS布局笔记一(代码实现自动布局)
- ios 代码 自动布局(一)
- ios 代码 自动布局(二)
- iOS开发笔记--使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- ios_基础篇3_关于xib布局和代码布局
- iOS开发笔记--使用Auto Layout中的VFL(Visual format language)--代码实现自动布局