- (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]; }
#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).
Symbol | Replacement rule |
---|---|
<visualFormatString> | (<orientation>:)? (<superview><connection>)? <view>(<connection><view>)* (<connection><superview>)? |
<orientation> |
|
<superview> |
|
<view> |
|
<connection> | e| |
<predicateList> | <simplePredicate>|<predicateListWithParens> |
<simplePredicate> | <metricName>|<positiveNumber> |
<predicateListWithParens> |
|
<predicate> | (<relation>)?(<objectOfPredicate>)( |
<relation> |
|
<objectOfPredicate> | <constant>|<viewName> (see note) |
<priority> | <metricName>|<number> |
<constant> | <metricName>|<number> |
<viewName> | Parsed as a C identifier. This must be a key mapping to an instance of |
<metricName> | Parsed as a C identifier. This must be a key mapping to an instance of |
<number> | As parsed by |
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.
参考自:
http://developer.apple.com/library/mac/#documentation/UserExperience/Conceptual/AutolayoutPG/Articles/formatLanguage.html