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
Set UITableView content inset permanently
提问by cojoj
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 UISearchDisplayController
etc. So when I call:
在我的应用程序中,我有一个UISearchBar
under,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 contentInset
permanently 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
你要在View Controller'sAttribute Inspector中
照顾这个小家伙
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
:
必须取消选中它,以便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. automaticallyAdjustsScrollViewInsets
has been deprecated and right now to achieve similar effect you have to go to Size Inspectorwhere you can find this:
Also, you can achieve the same in code:
如果涉及到iOS 11和Xcode 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))