iOS:多行uilabel仅显示一行自动布局

时间:2013-02-24 23:46:17

标签: ios autolayout

下面的代码,我认为应该会产生一个多行标签后跟一个按钮,但是,在布局后,只有一行标签显示出来。虽然我可以在垂直布局中放置一个明确的高度,但会破坏目的。关于我应该应用哪些其他限制的任何想法?

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)]];

4 个答案:

答案 0 :(得分:38)

添加以下行:

lbExplain.preferredMaxLayoutWidth = 280;

这必须是在IB中自动完成的事情,因为如果您在那里设置标签,标签会正确扩展。你传递它的数字似乎与它如何垂直扩展有关,但它不会覆盖你为宽度设置的约束。

编辑后:

如果我在updateViewConstraints中添加该行,并将该数字与视图的边界相关联,则效果会更好。这样它可以在旋转时正确调整。

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

答案 1 :(得分:1)

此处的问题是preferredMaxLayoutWidth未设置。因此,文本不受任何宽度的约束,因此,它不会换行到下一行。

我发现在Autolayout中使用多行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

答案 2 :(得分: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

答案 3 :(得分:0)

我遇到了类似的问题,UITableViewCell中的多行标签(使用AutoLayout定位)不会自行扩展。

在UITableView内部使用 UITableViewAutomaticDimension estimatedHeightForRowAtIndexPath 方法“自动”处理大小。 (我认为这可能是问题的根源)

我通过调用:

来解决这个问题
[cell layoutIfNeeded]
cellForRowAtIndexPath 方法中的

,就在返回单元格之前。

它就像一个魅力!

相关问题