调整的UITableView头和包含的UITextView(iOS7 +自动版式) [英] Resize UITableView Header AND containing UITextView (iOS7 + AutoLayout)

查看:107
本文介绍了调整的UITableView头和包含的UITextView(iOS7 +自动版式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在挣扎与此一小会儿,我想确保我这样做是正确的方式。作为参考,我用自动版式与故事板,这是一个iOS7 - 唯一的应用程序。

I've been struggling with this now for a little while, and I want to make sure I'm doing this the right way. For reference, I am using AutoLayout with Storyboards, and it's an iOS7-only app.

我有一个标题视图一个UITableView,并为HeaderView的UI在故事板挂钩。我留在故事板的头,而且它包含了一些元素,其中一个是不可滚动的UITextView。

I have a UITableView with a header-view, and the UI for the HeaderView is hooked up in the storyboard. I've left the header in the storyboard, and it contains a few elements, one of which is a non-scrollable UITextView.

下面是它是如何基本上看起来情节板中的截图:

Here is a screenshot of how it basically looks in the storyboard:

我黑暗的标题视图的一个暗灰色的背景,因此脱颖而出。

I darkened the header-view's background to a darker grey so it stands out.

的的UITextView的文本可以是动态的,因此它的高度需要依赖于文本的大小来改变。但棘手的问题是,该表的标题视图需要调整它的高度为好,根据该文本的大小。我试着限制了一些不同的东西,但它不是真正的正常工作(除非我禁用自动版式编程和重新大小的东西,这我真的想避免)。

The UITextView's text can be dynamic, so its height needs to change depending on the size of the text. But the tricky part is that the table's header-view needs to adjust its height as well, depending on this text's size. I've tried a few different things with constraints, but it's not really working correctly (unless I disable AutoLayout and re-size things programatically, which I really would like to avoid).

我想这是尽可能干净,用尽可能少code作为必要的,当然。我不依赖于使用表格视图头,虽然它与我的需求的效果很好,因为我想它和它包含的TextView与下面的实际细节中滚动。但是,这一切都这样说,如果有一个更简单的方法来实现这一点(即带有一个UILabel),我会很乐意改变底层结构。

I want this to be as clean as possible with as little code as necessary, of course. I am not tied to using the table-view header, although it works well with my needs, since I want it and its containing textview to scroll with the actual details below. But that all being said, if there is a more simple approach to accomplishing this (i.e. with a uilabel), I will gladly change the underlying structure.

有什么想法作为一种方法?不找人通过的答案,握住我的手;只是在寻找主要是针对一些很好的出发点如果可能的话。在此先感谢!

Any thoughts as to an approach? Not looking for someone to hold my hand through an answer; just looking mainly for some good starting points if possible. Thanks in advance!

推荐答案

前段时间我找到新的解决方案,可能需要调整的动画和是实验性的,但是,对于某些范围内它的罚款的情况下,所以给它一个尝试

Some time ago I found new solution, maybe it needs tweaking for animations and is experimental, but, for some range of cases it's fine, so give it a try:


  1. 添加到headerView一个UITableView。

  2. 添加子视图headerView,姑且称之为包装。

  3. 请包装的高度与它的子视图调整(通过自动
    布局)。

  4. 当自动布局说完了布局,设置headerView的高度
    包装的高度。 (见 -updateTableViewHeaderViewHeight

  5. 重新设置headerView。 (见 -resetTableViewHeaderView

  1. Add a headerView to a UITableView.
  2. Add a subview to headerView, let's call it wrapper.
  3. Make wrapper's height be adjusted with it's subviews (via Auto Layout).
  4. When autolayout had finished layout, set headerView's height to wrapper's height. (see -updateTableViewHeaderViewHeight)
  5. Re-set headerView. (see -resetTableViewHeaderView)

下面是一些code:

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    [self updateTableViewHeaderViewHeight];
}

/**
 tableView's tableViewHeaderView contains wrapper view, which height is evaluated
 with Auto Layout. Here I use evaluated height and update tableView's
 tableViewHeaderView's frame.

 New height for tableViewHeaderView is applied not without magic, that's why 
 I call -resetTableViewHeaderView.
 And again, this doesn't work due to some internals of UITableView, 
 so -resetTableViewHeaderView call is scheduled in the main run loop.
*/
- (void)updateTableViewHeaderViewHeight
{
    // get height of the wrapper and apply it to a header
    CGRect Frame = self.tableView.tableHeaderView.frame;
    Frame.size.height = self.tableHeaderViewWrapper.frame.size.height;
    self.tableView.tableHeaderView.frame = Frame;

    // this magic applies the above changes
    // note, that if you won't schedule this call to the next run loop iteration
    // you'll get and error
    // so, I guess that's not a clean solution and could be wrapped in @try@catch block
    [self performSelector:@selector(resetTableViewHeaderView) withObject:self afterDelay:0];
}

// yeah, guess there's something special in the setter
- (void)resetTableViewHeaderView
{
    // whew, this could be animated!
    [UIView beginAnimations:@"tableHeaderView" context:nil];

        self.tableView.tableHeaderView = self.tableView.tableHeaderView;

    [UIView commitAnimations];
}

这一切最初的自动布局后传中无缝工作。以后,如果你改变包装的内容,以便获得不同的高度,它不会出于某种原因(猜铺设的UILabel需要几个自动布局通过或东西)。我解决了这个与调度setNeedsLayout为的ViewController在下次运行循环迭代视图。

All this works seamlessly after the initial autolayout pass. Later, if you change wrapper's contents so that it gains different height, it wont work for some reason (guess laying out UILabel requires several autolayout passes or something). I solved this with scheduling setNeedsLayout for the ViewController's view in the next run loop iteration.

我创建示例项目:<一href=\"https://github.com/maniak-dobrii/iOS-solutions/tree/master/TableHeaderView+Autolayout\">TableHeaderView+Autolayout.

这篇关于调整的UITableView头和包含的UITextView(iOS7 +自动版式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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