17

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

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

4 回答 4

39

添加行:

lbExplain.preferredMaxLayoutWidth = 280;

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

编辑后:

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

-(void)updateViewConstraints {
    [super updateViewConstraints];
    lbExplain.preferredMaxLayoutWidth = self.view.bounds.size.width - 40;
}
于 2013-02-25T01:00:03.850 回答
1

这里的问题preferredMaxLayoutWidth是没有设置。因此,文本不受任何宽度的限制,因此,它不会换行到下一行。

我发现在UILabelAutolayout 中使用多行的最简单方法是创建一个子类:

@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
于 2013-08-28T01:25:37.653 回答
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

于 2013-02-25T00:34:04.970 回答
0

我有一个类似的问题,UITableViewCell 中的多行标签,使用 AutoLayout 定位,不会自行扩展。

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

我通过调用解决了这个问题:

[cell layoutIfNeeded]

cellForRowAtIndexPath方法中,就在返回单元格之前。

它就像一个魅力!

于 2015-12-28T16:01:58.803 回答