ios 永久设置 UITableView 内容插入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22020943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:34:54  来源:igfitidea点击:

Set UITableView content inset permanently

iosobjective-cuitableviewuiscrollviewpull-to-refresh

提问by cojoj

In my app I have a UISearchBarunder UINavigationBarso it is always visible to user. In that case I had to set contentInsetwith extra 44px so the UIScrollViewwill 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:

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

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

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

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

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

所以我的问题是:如何contentInset永久设置,这样我就不必担心内部数据发生任何变化UITableView

回答by cojoj

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'sAttribute Inspector asvi

你要在View Controller'sAttribute Inspector中 照顾这个小家伙阿维

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

必须取消选中它,以便contentInset在任何更改后不会设置默认值。之后,它只是将单行添加到viewDidLoad

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


iOS 11, Xcode 9 update

iOS 11、Xcode 9 更新

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

如果涉及到iOS 11Xcode 9 ,看起来以前的解决方案不再是正确的解决方案。automaticallyAdjustsScrollViewInsets已被弃用,现在要实现类似的效果,您必须转到Size Inspector,在那里您可以找到: 此外,您可以在代码中实现相同的效果:在此处输入图片说明

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

回答by rmooney

In Swift:

在斯威夫特:

override func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews()
      self.tableView.contentInset = UIEdgeInsets(top: 108, left: 0, bottom: 0, right: 0)
}

回答by Matan Guttman

automaticallyAdjustsScrollViewInsets is deprecated in iOS11 (and the accepted solution no longer works). use:

AutomaticAdjustsScrollViewInsets 在 iOS11 中已弃用(并且已接受的解决方案不再有效)。用:

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

回答by Fran Martin

Add in numberOfRowsInSection your code [self.tableView setContentInset:UIEdgeInsetsMake(108, 0, 0, 0)];. So you will set your contentInset always you reload data in your table

添加 numberOfRowsInSection 您的代码[self.tableView setContentInset:UIEdgeInsetsMake(108, 0, 0, 0)];。因此,您将始终设置 contentInset 重新加载表中的数据

回答by Kappe

After one hour of tests the only way that works 100% is this one:

经过一小时的测试,100% 工作的唯一方法是:

-(void)hideSearchBar
{
    if([self.tableSearchBar.text length]<=0 && !self.tableSearchBar.isFirstResponder)
    {
        self.tableView.contentOffset = CGPointMake(0, self.tableSearchBar.bounds.size.height);
        self.edgesForExtendedLayout = UIRectEdgeBottom;
    }
}

-(void)viewDidLayoutSubviews
{
    [self hideSearchBar];
}

with this approach you can always hide the search bar if is empty

使用这种方法,如果搜索栏为空,您可以随时隐藏搜索栏

回答by Saeed Ir

This is how it can be fixed easily through Storyboard (iOS 11 and Xcode 9.1):

这是通过 Storyboard(iOS 11 和 Xcode 9.1)轻松修复的方法:

Select Table View > Size Inspector > Content Insets: Never

选择表格视图 > 尺寸检查器 > 内容插入:从不

回答by user3168555

Try setting tableFooterView tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNonzeroMagnitude))

尝试设置 tableFooterView tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNonzeroMagnitude))