iOS版:多行的UILabel只显示与自动布局一行 [英] iOS: multiline uilabel only shows one line with autolayout

查看:652
本文介绍了iOS版:多行的UILabel只显示与自动布局一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code以下,我觉得应该导致IIN一个多标签后跟一个按钮,不过,布局后有显示出来的标签只有一行。虽然我可以把一个明确的高度在垂直布局,将打败的目的。关于应适用什么其他的限制,我什么想法?

 的UILabel * lbExplain = [[的UILabel的alloc]初始化];
lbExplain.text = @同步过程,您可以将库中的不同设备之间同步通过点击下面的按钮,你可以找到其他设备与其它设备也必须运行此的一个应用来同步。
lbExplain.lineBreakMode = NSLineBreakByWordWrapping;
lbExplain.numberOfLines = 0;
lbExplain.translatesAutoresizingMaskIntoConstraints = NO;*的UIButton = btnPartner [UIButton的buttonWithType:UIButtonTypeRoundedRect];
[btnPartner的setTitle:@寻找合作伙伴forState:UIControlStateNormal];
[btnPartner addTarget:自我行动:@选择(findPartners :) forControlEvents:UIControlEventTouchUpInside];
btnP​​artner.translatesAutoresizingMaskIntoConstraints = NO;[self.view addSubview:lbExplain];
[self.view addSubview:btnPartner];[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@H:| - [lbExplain] - |选项​​:0的指标:无意见:NSDictionaryOfVariableBindings(lbExplain)];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@V:| - [lbExplain] - [btnPartner]选项:NSLayoutFormatAlignAllLeft指标:无意见:NSDictionaryOfVariableBindings(lbExplain,btnPartner)];


解决方案

添加一行:

  lbExplain preferredMaxLayoutWidth = 280。

这必须是东西是因为如果你将它设置有标签适当扩大在IB自动完成。你通过它的数字似乎无关紧要它垂直是如何扩展了一些,但它不会覆盖你的宽度设定的限制。

修改后:

这工作得更好,如果我加入updateViewConstraints该行,并涉及到视图的边界数。这样,它正确地调整轮换。

   - (无效){updateViewConstraints
    [超级updateViewConstraints]
    lbExplain preferredMaxLayoutWidth = self.view.bounds.size.width - 40。
}

The code below, I think should result iin a multiline label followed by a button, however, after layout there is only a single line of the label showing up. While I could put an explicit height in the vertical layout that would defeat the purpose. Any ideas about what other constraints I should apply?

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

解决方案

Add the line:

lbExplain.preferredMaxLayoutWidth = 280;

This must be something that is done automatically in IB, since the label expands properly if you set it up there. The number you pass it does seem to matter some in how it expands vertically, but it doesn't override the constraints you set for the width.

After Edit:

It works better if I add that line in updateViewConstraints, and relate the number to the view's bounds. This way it adjusts properly on rotation.

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

这篇关于iOS版:多行的UILabel只显示与自动布局一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆