IOS自动布局XIB和CODE,附代码和PPT

- (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]

image: ../Art/standardSpace.png
Width Constraint

[button(>=50)]

image: ../Art/widthConstraint.png
Connection to Superview

|-50-[orchidBox]-50-|

image: ../Art/connectionToSuperview.png
Vertical Layout

V:[topField]-10-[bottomField]

扫描二维码关注公众号,回复: 661499 查看本文章
image: ../Art/verticalLayout.png
Flush Views

[maroonView][oceanView]

image: ../Art/flushViews.png
Priority

[button(100@20)]

image: ../Art/priority.png
Equal Widths

[button1(==button2)]

image: ../Art/equalWidths.png
Multiple Predicates

[flexibleButton(>=70,<=100)]

image: ../Art/multiplePredicates.png
A Complete Line of Layout

|-[find]-[findNext]-[findField(>=20)]-|

image: ../Art/completeLayout.png

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 fonte denotes the empty string).

Symbol

Replacement rule

<visualFormatString>

(<orientation>:)?

(<superview><connection>)?

<view>(<connection><view>)*

(<connection><superview>)?

<orientation>

H|V

<superview>

|

<view>

[<viewName>(<predicateListWithParens>)?]

<connection>

e|-<predicateList>-|-

<predicateList>

<simplePredicate>|<predicateListWithParens>

<simplePredicate>

<metricName>|<positiveNumber>

<predicateListWithParens>

(<predicate>(,<predicate>)*)

<predicate>

(<relation>)?(<objectOfPredicate>)(@<priority>)?

<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 NSView in the passed views dictionary.

<metricName>

Parsed as a C identifier. This must be a key mapping to an instance of NSNumber in the passed metrics dictionary.

<number>

As parsed by strtod_l, with the C locale.

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

 

猜你喜欢

转载自duchengjiu.iteye.com/blog/1906561