如何调整上海华以配合自动布局所有子视图? [英] How to resize superview to fit all subviews with autolayout?

查看:211
本文介绍了如何调整上海华以配合自动布局所有子视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自动布局的理解是,它需要的SuperView对约束和内在尺寸大小和底座,它计算的子视图位置。

My understanding of autolayout is that it takes the size of superview and base on constrains and intrinsic sizes it calculates positions of subviews.

有没有办法来扭转这一进程?我要调整约束和内在尺寸的基础上,上海华。什么是实现这一目标的最简单的方法?

Is there a way to reverse this process? I want to resize superview on the base of constrains and intrinsic sizes. What is the simplest way of achieving this?

我有鉴于X $ C $Ç我为的UITableView A头使用而设计的。该视图包括一个标签和一个按钮。标签的大小不同而不同的数据。根据限制了标签成功推动按钮向下或者如果有按钮和上海华的底部之间的约束的标签为com pressed

I have view designed in Xcode which I use as a header for UITableView. This view includes a label and a button. Size of the label differs depending on data. Depending on constrains the label successfully pushes the button down or if there is a constrain between the button and bottom of superview the label is compressed.

我已经找到了一些类似的问题,但他们没有很好的和简单的答案。

I have found a few similar questions but they don’t have good and easy answers.

推荐答案

要使用正确的API是的UIView systemLayoutSizeFittingSize:,传球或者 UILayoutFittingCom pressedSize UILayoutFittingExpandedSize

The correct API to use is UIView systemLayoutSizeFittingSize:, passing either UILayoutFittingCompressedSize or UILayoutFittingExpandedSize.

对于一个正常的的UIView 使用自动布局这应该只是工作,只要你的约束条件是正确的。如果你想用它在的UITableViewCell (确定例如行高度),那么你应该把它对着你的细胞内容查看并抓住高度。

For a normal UIView using autolayout this should just work as long as your constraints are correct. If you want to use it on a UITableViewCell (to determine row height for example) then you should call it against your cell contentView and grab the height.

进一步考虑。对于这些是imperitive的 preferredMaxLayoutWidth 属性设置正确,这样的标签提供了一个正确的 intrinsicContentSize ,这将在 systemLayoutSizeFittingSize的计算中使用。

Further considerations exist if you have one or more UILabel's in your view that are multiline. For these it is imperitive that the preferredMaxLayoutWidth property be set correctly such that the label provides a correct intrinsicContentSize, which will be used in systemLayoutSizeFittingSize's calculation.

编辑:按要求,例如增加高度计算为一个表格视图单元格

使用自动布局为表单元格高度计算是不是超级有效,但可以肯定的是方便,特别是如果你有,有一个复杂的布局单元格。

Using autolayout for table-cell height calculation isn't super efficient but it sure is convenient, especially if you have a cell that has a complex layout.

正如我前面所说,如果您使用的是多行的UILabel 它的当务之急同步的 preferredMaxLayoutWidth 的标签宽度。我使用自定义的的UILabel 子类来做到这一点:

As I said above, if you're using a multiline UILabel it's imperative to sync the preferredMaxLayoutWidth to the label width. I use a custom UILabel subclass to do this:

@implementation TSLabel

- (void) layoutSubviews
{
    [super layoutSubviews];

    if ( self.numberOfLines == 0 )
    {
        if ( self.preferredMaxLayoutWidth != self.frame.size.width )
        {
            self.preferredMaxLayoutWidth = self.frame.size.width;
            [self setNeedsUpdateConstraints];
        }
    }
}

- (CGSize) intrinsicContentSize
{
    CGSize s = [super intrinsicContentSize];

    if ( self.numberOfLines == 0 )
    {
        // found out that sometimes intrinsicContentSize is 1pt too short!
        s.height += 1;
    }

    return s;
}

@end

下面是一个人为的UITableViewController子类展示heightForRowAtIndexPath:

Here's a contrived UITableViewController subclass demonstrating heightForRowAtIndexPath:

#import "TSTableViewController.h"
#import "TSTableViewCell.h"

@implementation TSTableViewController

- (NSString*) cellText
{
    return @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
}

#pragma mark - Table view data source

- (NSInteger) numberOfSectionsInTableView: (UITableView *) tableView
{
    return 1;
}

- (NSInteger) tableView: (UITableView *)tableView numberOfRowsInSection: (NSInteger) section
{
    return 1;
}

- (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath
{
    static TSTableViewCell *sizingCell;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        sizingCell = (TSTableViewCell*)[tableView dequeueReusableCellWithIdentifier: @"TSTableViewCell"];
    });

    // configure the cell
    sizingCell.text = self.cellText;

    // force layout
    [sizingCell setNeedsLayout];
    [sizingCell layoutIfNeeded];

    // get the fitting size
    CGSize s = [sizingCell.contentView systemLayoutSizeFittingSize: UILayoutFittingCompressedSize];
    NSLog( @"fittingSize: %@", NSStringFromCGSize( s ));

    return s.height;
}

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
    TSTableViewCell *cell = (TSTableViewCell*)[tableView dequeueReusableCellWithIdentifier: @"TSTableViewCell" ];

    cell.text = self.cellText;

    return cell;
}

@end

一个简单的自定义单元格:

A simple custom cell:

#import "TSTableViewCell.h"
#import "TSLabel.h"

@implementation TSTableViewCell
{
    IBOutlet TSLabel* _label;
}

- (void) setText: (NSString *) text
{
    _label.text = text;
}

@end

和,这里的故事板中定义的约束的图片。请注意,有标签上没有高度/宽度的限制 - 这些都是从推断标签的 intrinsicContentSize

And, here's a picture of the constraints defined in the Storyboard. Note that there are no height/width constraints on the label - those are inferred from the label's intrinsicContentSize:

这篇关于如何调整上海华以配合自动布局所有子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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