简体   繁体   中英

Empty white space above UITableView inside a UIView

I'm trying to position a TableView inside my ViewController view but leaving a 44 height gap between the bottom of the navigation bar and the top of the table. I then wanted to place a UITextField inside that gap to act as a stationary header. For some reason, the TableView has an empty white space above the start of the "Prototype Cells". Its just white space. Here is what it looks like in the storyboard.

故事板

When viewing the app display, this is what it looks like:

应用程序1

When scrolling the table, it goes all the way up to the correct place:

应用程序2

Try to look in the 'attribute inspector' (in the right menu) of the Participants ViewController. Check for the option 'Extend Edges' and uncheck the 'Under Top Bars', and then relocate your tableview.

Another possible solution is to just uncheck the option 'Adjust Scroll View Insets' . The reason is that when the viewController extends its edges, let's say under the top bar, the tableView's scrollView automatically adjusts its inset top, so that the content of the tableView will start exactly under the top bar. But in your case this is not needed, since your tableView itself starts under the bar.

Focus on the ViewController and got to the Attribute Inspector tab:

在此输入图像描述

试试这个:

self.tableView.contentInset = UIEdgeInsetsMake(-65, 0, 0, 0)

只需在ViewDidLoad方法中添加它即可

self.automaticallyAdjustsScrollViewInsets = NO;

You should not need to change the default setting for Extend Edges.

Looks like the real problem is a blank table header view in your storyboard. It's showing in the screenshot you provided of your storyboard, right below the Enter Name view and right above the Prototype Cells view. Delete it.

I just found a solution for this. In my case, i was using TabBarViewController, A just uncheck the option 'Adjust Scroll View Insets'. Issues goes away.

https://i.stack.imgur.com/qgQI8.png

对于Objective-C人员,请在ViewDidLoad方法中添加此项。

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

My issues is, I set tableHeaderView as new UIView like this

self.tableHeaderView = UIView(frame: .zero)

Remove this line, the issue is gone. Try this:

//self.tableHeaderView = UIView(frame: .zero)

change your table view style from grouped to plain

如图所示

This is the 2022, iOS 15 solution if anyone stumbles upon this.

if #available(iOS 15.0, *) {
    UITableView.appearance().sectionHeaderTopPadding = CGFloat(0)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM