ios UITableView 底部的额外空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28641280/
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
UITableView extra space at bottom
提问by Guilherme Torres Castro
My tableview has an extra space at the bottom, as you see in the picture:
我的 tableview 在底部有一个额外的空间,如图所示:
All the rows in the tableView have a fixed height of 71pt.
tableView 中的所有行都具有 71pt 的固定高度。
回答by Guilherme Torres Castro
回答by Integrating Stuff
In my case this was due to having a footer of height 18 by default at the bottom of the UITableView. Setting it to 1 removed the extra space at the bottom.
在我的例子中,这是因为 UITableView 的底部默认有一个高度为 18 的页脚。将其设置为 1 删除了底部的额外空间。
回答by Saeed Ir
This is how it can be fixed easily through Storyboard (iOS 11):
这是它可以通过 Storyboard (iOS 11) 轻松修复的方法:
Select Table View > Size Inspector > Content Insets: Never
选择表格视图 > 尺寸检查器 > 内容插入:从不
回答by iOS Unit
In my case tableView was created programmaticaly and I had to set tableView.estimatedRowHeight
to make the space disappear
在我的情况下,tableView 是以编程方式创建的,我必须设置tableView.estimatedRowHeight
以使空间消失
回答by urgentx
I had this issue when I flipped my UITableView upside-down for a chat messaging screen.
当我将 UITableView 倒置以显示聊天消息屏幕时,我遇到了这个问题。
Before I flipped the table view, the content inset was leaving a blank space at the top so that when we scrolled all the way up, the content was not obstructed by the navigation bar. Once I flipped it the inset was moved to the bottom.
在我翻转 table view 之前,content inset 在顶部留下了一个空白区域,这样当我们一直向上滚动时,内容不会被导航栏挡住。一旦我翻转它,插图就移到了底部。
Change your 'Content Insets' to Never in the Size Inspector of your UITableView to get rid of this space at the end.
在 UITableView 的 Size Inspector 中将“Content Insets”更改为 Never 以在最后摆脱此空间。
回答by Varsha Shivhare
in this case you can give tableview the bottom space 0 to its superview and it will work i think or you can do,
在这种情况下,您可以为 tableview 为其 superview 提供底部空间 0,我认为它会起作用,或者您可以这样做,
tableView.tableFooterView = UIView();
tableView.tableFooterView = UIView();
or you can do like below,
或者你可以像下面那样做,
The lower value should be 1.0.
较低的值应为 1.0。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger )section {
if(section == 0) {
return 6;
} else {
return 1.0;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger )section {
return 5.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger )section {
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger )section {
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}
回答by Sirens
@urgentx's answerdid most of what I wanted but didn't get all the way there. I'm in a similar situation (upside down UITableView for a chat app) however their suggestion fully disables safe area behavior which isn't desirable on devices like the iPhone X because it means the tableview will be clipped by both the home indicator and any top navigation bars.
@urgentx 的回答完成了我想要的大部分工作,但并没有完全实现。我处于类似的情况(聊天应用程序的 UITableView 颠倒)但是他们的建议完全禁用安全区域行为,这在 iPhone X 等设备上是不可取的,因为这意味着 tableview 将被 home 指示器和任何顶部导航栏。
I did the following in order to make the tableview still correctly avoid safe areas while inverted:
为了使 tableview 在倒置时仍然正确避开安全区域,我做了以下操作:
override func viewDidLoad() {
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
self.tableView.contentInsetAdjustmentBehavior = .never
}
and then:
进而:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
//fetch the safe area in layoutSubviews since this it's possible that they can change when the device is rotated
let safeArea = self.tableView.safeAreaInsets
//insets are **flipped** because the tableview is flipped over its Y-axis!
self.tableView.contentInset = .init(top: safeArea.bottom, left: 0, bottom: safeArea.top, right: 0)
}
This in essence replicates contentInsetAdjustmentBehavior = .automatic
behavior but with the insets flipped across the Y-axis.
这实质上复制了contentInsetAdjustmentBehavior = .automatic
行为,但插图在 Y 轴上翻转。
回答by Dmitry
The accepted solution is no longer a correct one if it comes to iOS 11 and Xcode 9. To achieve similar effect you have to go to Size Inspector where you can find this:
如果涉及到 iOS 11 和 Xcode 9,已接受的解决方案不再是正确的解决方案。要实现类似的效果,您必须转到 Size Inspector,您可以在其中找到:
You can achieve the same in code:
您可以在代码中实现相同的目标:
if #available(iOS 11.0, *) {
scrollView.contentInsetAdjustmentBehavior = .never
} else {
automaticallyAdjustsScrollViewInsets = false
}
回答by Brian Hong
I had the same issue, with latest Xcode (11.3) with latest iOS (13.3) iPhone. I tried many answers, but none worked.
我有同样的问题,最新的 Xcode (11.3) 和最新的 iOS (13.3) iPhone。我尝试了很多答案,但没有一个奏效。
My tableView
has top, bottom, leading, and trail constraints but somehow there is large space at bottom below the last cell. (tableView.contentSize.height
was almost double of what is needed) Data for tableView
is not dynamic at all.It happens both when the view controller that has it shows it and when tableView
is hidden then shown again.
我tableView
有顶部、底部、前导和尾迹约束,但不知何故,最后一个单元格下方的底部有很大的空间。(tableView.contentSize.height
几乎是所需数据的两倍)Data fortableView
根本不是动态的。它发生在拥有它的视图控制器显示它时和tableView
隐藏然后再次显示时。
I solved it by using scrollView's delegate method like below, so that when the user starts dragging, height is adjusted. For me, it works so great, hope for you, too!
我通过使用如下所示的 scrollView 的委托方法解决了它,以便在用户开始拖动时调整高度。对我来说,它太棒了,希望你也是!
// this is the best place to adjust the contentSize.height of tableView.
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
print("scrollViewWillBeginDragging():") // optional
// replace allParkingGeos.count with your data for tableView
tableView.contentSize.height = CGFloat(allParkingGeos.count) * cellHeight
}
回答by Mehrdad
My problem didn't solve by the answers.
i had a UISearchbar
as tableHeaderView
which caused the white space ( i don't know why) . I removed the searchbar from tableview in storyboard and in viewWillLayoutSubviews
added in table header then problem solved.
我的问题没有通过答案解决。我有一个UISearchbar
作为tableHeaderView
造成的空白空间(我不知道为什么)。我从故事板viewWillLayoutSubviews
中的表格视图中删除了搜索栏,并在表格标题中添加了搜索栏,然后问题解决了。
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
tableView.tableHeaderView = searchBar
}