Swift: Tableview scrolls under navigation bar but

2019-07-14 02:29发布

问题:

I hid the navigation bar's shadow using the following trick:

    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController?.navigationBar.shadowImage = UIImage()

I also have the following set:

    self.extendedLayoutIncludesOpaqueBars = true
    self.automaticallyAdjustsScrollViewInsets = true
    self.tabBarController?.tabBar.isHidden = true

Everything looks fine on my page, except that when I scroll up my tableView it goes under the navigation bar as expected, but above the status bar:

How do I make sure that the tableView scrolls under both the navigation bar and status bar?

回答1:

This will place the UITableView below the status bar.

self.edgesForExtendedLayout=UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars=NO;
self.automaticallyAdjustsScrollViewInsets=NO;


回答2:

You hid the default background of the navigation bar so this is to be expected. It means you are creating the green background in the navigation bar by yourself. You have to manually extend it to cover both status bar and navigation bar which is what the default background would do.

Or use a green image instead of a transparent one.



回答3:

You can try table view as under the UIEdgeInsetMake and also its scroll view

tableView.contentInset = UIEdgeInsetsMake(20.0, 0.0, 0.0, 0.0)
tableView.scrollIndicatorInsets = UIEdgeInsetsMake(20.0, 0.0, 0.0, 0.0)