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

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

问题描述

在我的应用中,我在UINavigationBar下有一个UISearchBar,因此它始终对用户可见.在那种情况下,我必须将contentInset设置为额外的44像素,以便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.

您必须在 View Controller的属性检查器中照顾这个小家伙

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 11 Xcode 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天全站免登陆