多行的UIButton和自动布局 [英] Multiline UIButton and autolayout

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

问题描述

我创建看起来像这样的一个视图控制器:

I have created a view controller that looks like this:

我希望两个顶部按钮总是具有自身和整个视图的左/右边缘之间20分。他们应该始终具有相同的宽度了。我已经创造了这一切的约束和它的工作原理正是我想要它。问题是垂直约束。按钮应始终顶部边缘下方20点。它们应具有相同的高度。然而,自动布局不尊重左侧的标签需要两行以适应其所有文字,所以结果看起来是这样的:

I want the two top buttons to always have 20 points between themselves and the left/right edges of the whole view. They should always have the same width too. I have created the constraints for all of this and it works exactly how I want it to. The problem is the vertical constraints. The buttons should always be 20 points beneath the top edge. They should have the same height. However, autolayout doesn't respect that the left label needs two lines to fit all its text, so the result looks like this:

我希望它看起来像在第一张照片。因为当应用程序在iPad上运行,只需要一个行中我不能添加恒定高度限制的按钮,这将是浪费有额外的空间即可。

I want it to look like in the first picture. I can't add constant height constraints to the buttons because when the app runs on iPad, only one line is needed and it would be wasteful to have extra space then.

viewDidLoad中我想这一点:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.leftButton.titleLabel.preferredMaxLayoutWidth = (self.view.frame.size.width - 20.0 * 3) / 2.0;
    self.rightButton.titleLabel.preferredMaxLayoutWidth = (self.view.frame.size.width - 20.0 * 3) / 2.0;
}

但是,这并没有改变anyhting。

But that did not change anyhting at all.

的问题:如何让我自动布局就该左键需要两行

推荐答案

我在哪里,我想我的按钮与标题一起成长同样的问题。我不得不sublcass的的UIButton intrinsicContentSize ,使其返回标签的固有大小。

I had the same problem where I wanted my button to grow along with its title. I had to sublcass the UIButton and its intrinsicContentSize so that it returns the intrinsic size of the label.

- (CGSize)intrinsicContentSize
{
    return self.titleLabel.intrinsicContentSize;
}

由于的UILabel 是多行,它的 intrinsicContentSize 是未知的,你必须将它 preferredMaxLayoutWidth See有关 objc.io文章

Since the UILabel is multiline, its intrinsicContentSize is unknown and you have to set its preferredMaxLayoutWidth See objc.io article about that

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.titleLabel.preferredMaxLayoutWidth = self.titleLabel.frame.size.width;
    [super layoutSubviews];
}

的布局的其余部分应该工作。如果您设置按钮都具有相等的高度,另外一个将增长到。完整的按钮看起来像这样

The rest of the layout should work. If you set your both button having equal heights, the other one will grow to. The complete button looks like this

@implementation TAButton

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    }
    return self;
}

- (CGSize)intrinsicContentSize
{
    return self.titleLabel.intrinsicContentSize;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.titleLabel.preferredMaxLayoutWidth = self.titleLabel.frame.size.width;
    [super layoutSubviews];
}

@end

这篇关于多行的UIButton和自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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