永久设置 UITableView 内容插入 [英] Set UITableView content inset permanently

查看:23
本文介绍了永久设置 UITableView 内容插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我在 UINavigationBar 下有一个 UISearchBar,因此它始终对用户可见.在这种情况下,我必须将 contentInset 设置为额外的 44px,以便 UIScrollView 将在 UISearchBar 下滚动(就像在 Contacts.app 中一样)).静态 UITableView 没有问题,但在我的情况下,我必须重新加载它的内容,切换到 UISearchDisplayController 等.所以当我打电话时:

In my app I have a UISearchBar under UINavigationBar so it is always visible to user. In that case I had to set contentInset with extra 44px so the UIScrollView will be scrolling under UISearchBar (exactly like in Contacts.app). And there would be no problem with static UITableView's but in my case I have to reload it's contents, switch to UISearchDisplayControlleretc. So when I call:

[self.tableView setContentInset:UIEdgeInsetsMake(108, 0, 0, 0)];

一切正常,直到例如我拉刷新...(为此我使用 SSPullToRefresh).

Everything works until e.g. I pull to refresh... (for this I use SSPullToRefresh).

所以我的问题是:如何永久设置 contentInset 以便我不必担心 UITableView 中的数据发生任何变化?

So my question is: How can I set contentInset permanently so I wouldn't have to worry about any changes happening to data inside UITableView?

推荐答案

可能是我的某种错误,因为我弄乱了自动布局和故事板,但我找到了答案.

Probably it was some sort of my mistake because of me messing with autolayouts and storyboard but I found an answer.

你必须在视图控制器的属性检查器中照顾这个小家伙

You have to take care of this little guy in View Controller's Attribute Inspector

必须取消选中它,以便在任何更改后不会设置默认的 contentInset.之后它只是向 viewDidLoad 添加一行代码:

It must be unchecked so the default contentInset wouldn't be set after any change. After that it is just adding one-liner to viewDidLoad:

[self.tableView setContentInset:UIEdgeInsetsMake(108, 0, 0, 0)]; // 108 is only example

<小时>

iOS 11、Xcode 9 更新

对于 iOS 11Xcode 9,看来之前的解决方案不再是正确的解决方案.automaticallyAdjustsScrollViewInsets 已被弃用,现在要实现类似的效果,您必须转到 Size Inspector ,在那里您可以找到:
此外,您可以在代码中实现相同的目标:

Looks like the previous solution is no longer a correct one if it comes to iOS 11 and Xcode 9. automaticallyAdjustsScrollViewInsets has been deprecated and right now to achieve similar effect you have to go to Size Inspector where you can find this:
Also, you can achieve the same in code:

if #available(iOS 11.0, *) {
    scrollView.contentInsetAdjustmentBehavior = .never
} else {
    automaticallyAdjustsScrollViewInsets = false
}

这篇关于永久设置 UITableView 内容插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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