如何使的UITableView的高度动态的,自动布局功能? [英] How to make UITableView's height dynamic with autolayout feature?

查看:154
本文介绍了如何使的UITableView的高度动态的,自动布局功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在X code 5使用自动布局。

I am using autolayout in Xcode 5.

我设置了​​表视图的高度大于或等于200像素。我想,它具有动态大小。因为有时会有许多行,有时会有几行。

I set the table view's height to Greater than or equal to 200px. I want that it has dynamic size. Because sometimes it will have many rows, sometimes it will have a few rows.

但大小始终是200像素。而如果含量比大,我应该向下滚动以查看下排

But the size is always 200px. And if the content is larger than that, I should scroll down to see the lower rows.

我应该怎么做给tableview中动态大小?

What should I do to give the tableview dynamic size?

推荐答案

这是利用X code 4.6和iPhone,但我相信它会X code 5的工作。

This is with Xcode 4.6 and the iPhone, but I believe it will work in Xcode 5.

1)在你的表视图X code单击以选中它。

1)In Xcode click on you table view to select it.

2)在工具窗格(参见图1)选择表格视图高度的限制,点击小齿轮图标,然后选择提升用户的约束。图标联合国右边会由紫色改变颜色为蓝色(见图2)

2)In the Utilities pane (see picture 1) select the constraint for the table view height, click on the little gear icon and choose "Promote to user constraint". The icon un the right will change color from purple to blue (see picture 2)

3)在显示的表视图中创建一个出口此约束视图控制器:

3)In the view controller that displays the table view create an outlet to this constraint:

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *dynamicTVHeight;

4)也把这个code在同一个视图控制器:

4)Also put this code in the same view controller:

- (void)viewWillAppear:(BOOL)animated
{
    // just add this line to the end of this method or create it if it does not exist
    [self.tableView reloadData]; 
}

-(void)viewDidLayoutSubviews
{
    CGFloat height = MIN(self.view.bounds.size.height, self.tableView.contentSize.height);
    self.dynamicTVHeight.constant = height;
    [self.view layoutIfNeeded];
}

这应该解决您的问题。

更新
可以省略步骤2)为X code,当你创建一个出口,它会自动促进约束用户的限制。

Update You can omit step 2) as Xcode automatically promotes the constraint to user constraint when you create an outlet to it.

这是给你想要做什么的示例项目的链接。下载它,并与X code 5,它应该工作测试。

This is a link to a sample project that does what you want. Download it and test it with Xcode 5. It should work.

<一个href=\"https://github.com/jcatalan007/TestTableviewAutolayout\">https://github.com/jcatalan007/TestTableviewAutolayout

有关X code 5的外观到文档如何在界面生成器访问的约束。

For Xcode 5 look into the documentation for how you can access the constraint in Interface Builder.

这篇关于如何使的UITableView的高度动态的,自动布局功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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