2013-02-24 41 views
15

下面的代碼,我認爲應該在一個多行標籤後面跟一個按鈕,然而,在佈局之後,只有一行標籤顯示出來。雖然我可以在垂直佈局中明確表示高度,但這會破壞目的。關於我應該採用哪些其他限制的任何想法?iOS:多行uilabel只顯示一行自動佈局

UILabel *lbExplain = [[UILabel alloc] init]; 
lbExplain.text = @"The Sync process allows you to synchronize your library between different devices. By clicking on the button below you can find other devices to sync with. The other device also has to be running this applicaton."; 
lbExplain.lineBreakMode = NSLineBreakByWordWrapping; 
lbExplain.numberOfLines = 0; 
lbExplain.translatesAutoresizingMaskIntoConstraints = NO; 

UIButton *btnPartner = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[btnPartner setTitle:@"Look for Partners" forState:UIControlStateNormal]; 
[btnPartner addTarget:self action:@selector(findPartners:) forControlEvents:UIControlEventTouchUpInside]; 
btnPartner.translatesAutoresizingMaskIntoConstraints = NO; 

[self.view addSubview:lbExplain]; 
[self.view addSubview:btnPartner]; 

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[lbExplain]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(lbExplain)]]; 
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[lbExplain]-[btnPartner]" options:NSLayoutFormatAlignAllLeft metrics:nil views:NSDictionaryOfVariableBindings(lbExplain, btnPartner)]]; 
+0

如果您在標籤上調用'-sizeToFit',會發生什麼? – 2013-02-24 23:51:18

+0

沒有區別。 – ckh 2013-02-25 00:06:12

+0

我看不到缺少的東西。如果你在IB中做基本相同的事情,它可以正常工作。 – rdelmar 2013-02-25 00:26:27

回答

37

添加一行:

lbExplain.preferredMaxLayoutWidth = 280; 

這必須是東西,在IB自動完成的,因爲如果你在那裏設置標籤,標籤就會正確展開。你傳遞它的數量在垂直擴展方面似乎很重要,但是它不會覆蓋你爲寬度設置的約束。

編輯後:

它可以更好,如果我加入updateViewConstraints該行,並涉及到視圖的邊界數量。這樣它就可以在旋轉時正確調整。

-(void)updateViewConstraints { 
    [super updateViewConstraints]; 
    lbExplain.preferredMaxLayoutWidth = self.view.bounds.size.width - 40; 
} 
+0

謝謝!這實際上工作,雖然我有點神祕,爲什麼? 280看起來像一個神奇的數字,但它適用於iPad和iPhone(當然iPad的寬度超過280)。 – ckh 2013-02-25 01:29:48

+0

即使旋轉,它是否真的可以在iPad上使用? 280僅爲320減去您設置的兩側20個點的空間(默認值)。我注意到,在iPhone上旋轉時,標籤以文本上方和下方的空格結束。 – rdelmar 2013-02-25 01:46:53

+0

@ckh,看我的編輯,我認爲這是一個更好的方法。 – rdelmar 2013-02-25 01:50:11

0

你確定沒有模棱兩可的佈局嗎?

你可以做一個測試你的代碼一樣

if ([self.view hasAmbiguousLayout]) { 
    NSLog(@"ambiguous"); 
} 

或使模擬器或測試設備上暫停並鍵入控制檯:

po [[UIWindow keyWindow] _autolayoutTrace] 

如果是的話,你可能會想嘗試做您的禁忌更像一點(乍一看,身高不明確):

@"V:|-[lbExplain]-[btnPartner]-|" 

@"V:|-[lbExplain(==300)]-[btnPartner(==200)]" 

這裏是文檔的視覺形式語言:

http://developer.apple.com/library/mac/#documentation/UserExperience/Conceptual/AutolayoutPG/Articles/formatLanguage.html

+0

OP的限制並不含糊,因爲標籤和按鈕具有固有的高度。至於你的第二個建議,那就是他想要避免的(設置一個明確的高度)。 – rdelmar 2013-02-25 00:48:40

+0

「UILabel」只有一行時才具有固有大小。 – 2016-09-27 14:48:28

1

這裏的問題是沒有設置preferredMaxLayoutWidth。因此,文本不受任何寬度限制,因此它不會換行到下一行。

我發現,最簡單的方法使用一個多UILabel與自動佈局是創建一個子類:

@interface MyMultilineLabel : UILabel 

@end 


@implementation MyMultilineLabel 

- (void)setBounds:(CGRect)bounds { 
    [super setBounds:bounds]; 
    CGFloat width = CGRectGetWidth(bounds); 
    if (self.preferredMaxLayoutWidth != width) { 
     self.preferredMaxLayoutWidth = width; 
    } 
} 

- (id)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.lineBreakMode = NSLineBreakByWordWrapping; 
     self.numberOfLines = 0; 
    } 
    return self; 
} 

@end 
0

我也有類似的問題,在一個多標籤在一個UITableViewCell,定位自動版式,不會擴大自己。

這是一個UITableView內部處理尺寸 '自動' 與UITableViewAutomaticDimensionestimatedHeightForRowAtIndexPath方法。(我認爲這可能是問題的來源)

我通過調用想通了這個問題:在的cellForRowAtIndexPath方法

[cell layoutIfNeeded] 

,只是返回單元格之前。

它的工作就像一個魅力!