2014-03-19 62 views
1

我正在拼命地將兩個視圖下面的對齊與自動佈局希望在未來的動畫和相對位置這使得它比剛剛設置框架和更新等更僵化。 。使用自動註銷將兩個uiviews彼此對齊使用自動註銷

因此,代碼:

// create two views 
UIView *one = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; 
UIView *two = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 50, 50)]; 

// add some color 
[one setBackgroundColor:[UIColor whiteColor]]; 
[two setBackgroundColor:[UIColor redColor]]; 

// add them to self (self = uiview) 
[self addSubview:one]; 
[self addSubview:two]; 

// make dict 
NSDictionary *views = NSDictionaryOfVariableBindings(one, two); 

// add constraints to make the views as wide as the container (self) 
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[one]|" 
                  options:0 
                  metrics:nil 
                   views:views]]; 

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[two]|" 
                  options:0 
                  metrics:nil 
                   views:views]]; 

// add constraint to make the second (two) item 10px below the first (one) 
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[one]-10-[two]" 
                  options:0 
                  metrics:nil 
                   views:views]]; 

```

enter image description here < - 結果

enter image description here < - 目標

我在做什麼錯?

在此先感謝!

+0

所以更容易使用Interface Builder做到這一點。 – PaulG

回答

3

這兩行解釋這一切:

UIView *one = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; 
UIView *two = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 50, 50)]; 

這是你看到什麼的結果:兩個視圖的尺寸50×50,和圖2被放置在x = 0和y = 150。

使用自動佈局時,您需要忘記所有關於幀和反射的東西。說明你想要的約束,對於每個視圖和視圖之間的約束,並讓autolayout在幕後創建框架。

聲明意見的自動佈局是這樣的:

UIView *one = [[UIView alloc] init]; 
one.translatesAutoresizingMaskIntoConstraints = NO; 
UIView *two = [[UIView alloc] init]; 
two.translatesAutoresizingMaskIntoConstraints = NO; 
+0

對不起,延遲迴復,但它的作品!非常感謝!! –

+0

很高興有幫助:-) – Moonwalkr

1

使用約束時,不應設置框架。這就是約束條件。 約束是原因,幀的效果

// create two views 
UIView *one = [[UIView alloc] init]; 
UIView *two = [[UIView alloc] init]; 
one.translatesAutoresizingMaskIntoConstraints = NO; 
two.translatesAutoresizingMaskIntoConstraints = NO; 

// add some color 
[one setBackgroundColor:[UIColor whiteColor]]; 
[two setBackgroundColor:[UIColor redColor]]; 

// add them to self (self = uiview) 
[self addSubview:one]; 
[self addSubview:two]; 

// make dict 
NSDictionary *views = NSDictionaryOfVariableBindings(one, two); 

// add constraints to make the views as wide as the container (self) 
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[one]|" 
                  options:0 
                  metrics:nil 
                   views:views]]; 

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[two]|" 
                  options:0 
                  metrics:nil 
                   views:views]]; 

// add constraint to make the second (two) item 10px below the first (one) 
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[one]-10-[two]|" 
                  options:0 
                  metrics:nil 
                   views:views]]; 

注:我在最後一行改變的約束。你的約束沒有指定視圖二的高度。它會給你一個「AMBIGUOUS」的佈局。