您的位置:首页 > 移动开发 > IOS开发

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: