NSLayoutConstraint-代碼實現自動佈局的函數用法說明

[NSLayoutConstraint constraintWithItem:(id)itemios

                             attribute:(NSLayoutAttribute)attributeiphone

                             relatedBy:(NSLayoutRelation)relation函數

                                toItem:(id)otherItemspa

                             attribute:(NSLayoutAttribute)otherAttribute.net

                            multiplier:(CGFloat)multiplier翻譯

                              constant:(CGFloat)constant]code



參數說明:
orm

第一個參數:指定約束左邊的視圖view1blog

第二個參數:指定view1的屬性attr1,具體屬性見文末。
ip

第三個參數:指定左右兩邊的視圖的關係relation,具體關係見文末。

第四個參數:指定約束右邊的視圖view2

第五個參數:指定view2的屬性attr2,具體屬性見文末。

第六個參數:指定一個與view2屬性相乘的乘數multiplier

第七個參數:指定一個與view2屬性相加的浮點數constant


這個函數的對照公式爲:

view1.attr1 <relation> view2.attr2 * multiplier + constant


注意:

1.若是你想設置的約束裏不須要第二個view,要將第四個參數設爲nil,第五個參數設爲NSLayoutAttributeNotAnAttribute


舉例:

1

2

3

4

5

6

7

[NSLayoutConstraint constraintWithItem:view1

                             attribute:NSLayoutAttributeLeft

                             relatedBy:NSLayoutRelationEqual

                                toItem:view2

                             attribute:NSLayoutAttributeRight

                            multiplier:1

                              constant:10]


翻譯過來就是:view1的左側,在,view2的右側,再多10個點,的地方。


附視圖的屬性和關係的值:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

typedef NS_ENUM(NSInteger, NSLayoutRelation) {

    NSLayoutRelationLessThanOrEqual = -1,          //小於等於

    NSLayoutRelationEqual = 0,                     //等於

    NSLayoutRelationGreaterThanOrEqual = 1,        //大於等於

};

typedef NS_ENUM(NSInteger, NSLayoutAttribute) {

    NSLayoutAttributeLeft = 1,                     //左側

    NSLayoutAttributeRight,                        //右側

    NSLayoutAttributeTop,                          //上方

    NSLayoutAttributeBottom,                       //下方

    NSLayoutAttributeLeading,                      //首部

    NSLayoutAttributeTrailing,                     //尾部

    NSLayoutAttributeWidth,                        //寬度

    NSLayoutAttributeHeight,                       //高度

    NSLayoutAttributeCenterX,                      //X軸中心

    NSLayoutAttributeCenterY,                      //Y軸中心

    NSLayoutAttributeBaseline,                     //文本底標線

                                                                                                                                                    

    NSLayoutAttributeNotAnAttribute = 0            //沒有屬性

};


NSLayoutAttributeLeft/NSLayoutAttributeRight 和NSLayoutAttributeLeading/NSLayoutAttributeTrailing的區別是left/right永遠是指左右,而leading/trailing在某些從右至左習慣的地區會變成,leading是右邊,trailing是左邊。



屏幕兼容的問題

爲了讓咱們的應用在不容尺寸的屏幕下都能 「正常」的表示,咱們儘可能不要把數據寫死。

大多數可視元素都是一個矩形區域,固然這個矩形區域有座標的,咱們有了這個區域座標就能肯定可視元素的現實位置了。

可是iphone5 和之前的屏幕不同了,在之前的設備中,咱們能夠添加一個 xx.@2x.png 來適應retina屏幕,可是iphoen5咋辦呢?

ios 引入了 Auto Layout 的東東,這個要和UIViewAutoresizing 區分下。

看下面代碼

- (void)viewDidLoad
{
    [super viewDidLoad];
        
    UIView *aView = [[UIView alloc] init];
    aView.backgroundColor = [UIColor redColor];

//爲了避免和autosizing衝突,咱們設置No

    [aView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:aView];
    
    UIView *bView = [[UIView alloc] init];
    bView.backgroundColor = [UIColor blueColor];
    [bView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:bView];
    
    NSDictionary *views = NSDictionaryOfVariableBindings(aView, bView);

//NSDictionaryOfVariableBindings  宏  其實 NSDictionaryOfVariableBindings(v1, v2, v3) 等效於 [NSDictionary dictionaryWithObjectsAndKeys:v1, @"v1", v2, @"v2", v3, @"v3", nil];    
    [self.view addConstraints:
    [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=50)-[aView(100)]"
                                             options:0
                                             metrics:nil
                                               views:views]];
    
    [self.view addConstraints:
     [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=100)-[aView(50)]"
                                             options:0
                                             metrics:nil
                                               views:views]];
   
    [self.view addConstraints:
     [NSLayoutConstraint constraintsWithVisualFormat:@"H:[bView(==aView)]"
                                             options:0
                                             metrics:nil
                                               views:views]];
    [self.view addConstraints:
     [NSLayoutConstraint constraintsWithVisualFormat:@"V:[bView(==aView)]"
                                             options:0
                                             metrics:nil
                                               views:views]];
    
    [self.view addConstraint:
     [NSLayoutConstraint constraintWithItem:bView
                                  attribute:NSLayoutAttributeLeft
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:aView
                                  attribute:NSLayoutAttributeRight
                                 multiplier:1
                                   constant:10]];
    //添加一個限制  等效於 bView.frame.origin.x  = (aView.frame.origin.x +aView.frame.size.width)  * 1  + 10,好像是這樣的!我的以爲!

  它是一種依賴關係,bView依賴aView,這樣就算aView變了,bView也會跟着變換。
    [self.view addConstraint:
     [NSLayoutConstraint constraintWithItem:bView
                                  attribute:NSLayoutAttributeTop
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:aView
                                  attribute:NSLayoutAttributeTop
                                 multiplier:1
                                   constant:0]];
    
    [aView release];
    [bView release];
}




constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:

Create a constraint of the form "view1.attr1 <relation> view2.attr2 * multiplier + constant".

屬性


最後的結果就是 「view1.attr1  <       >=    或者 ==   或者    <=       >  view2.attr2 * multiplier + constant」

相關文章
相關標籤/搜索