UITableView goes under translucent Navigation Bar

前端 未结 14 988
一整个雨季
一整个雨季 2020-12-22 23:51

I am trying to have a transparent navigation bar in IOS 7 app. There is a full screen image in my application. I am also having a UITableView over that image. When I use the

相关标签:
14条回答
  • 2020-12-23 00:18

    Only this code solves the problem:

      @IBOutlet weak var tableView: UITableView!
    
      func viewDidLoad() {
          super.viewDidLoad()
          self.tableView.contentInset = UIEdgeInsetsZero
      }
    
    0 讨论(0)
  • 2020-12-23 00:21

    Constrain the table view to the bottom of the navigation bar. The table view will automatically be offset by 44, but then in code we can just do this:

    tableView.contentInset = UIEdgeInsets(top: -44, left: 0, bottom: 0, right: 0)
    

    The bar is transparent and has no color, but the table view does not overlap it at all. Notice the word "Hook" gets cut off despite the navigation bar being transparent. This will only work of you constrain the table view top edge to be 0 from the navigation bar. NOT 0 from the top view.

    0 讨论(0)
  • 2020-12-23 00:22

    I had the similar problem for iOS 9. When I first open viewController, tableView is under top bar. Then after scrolling tableView everything works fine.

    1. Select your view controller
    2. Click the 'Attributes Inspector' tab
    3. Uncheck 'Under Top Bars'

    0 讨论(0)
  • 2020-12-23 00:25

    Solutions that introduce a magic constant don't scale most of the time. For example, if the next iPhone introduces a different navigation bar height we'll have to update our code.

    Fortunately, Apple provided us cleaner ways of overcoming this issue, for example topLayoutGuide:

    The topLayoutGuide property comes into play when a view controller is frontmost onscreen. It indicates the highest vertical extent for content that you don't want to appear behind a translucent or transparent UIKit bar (such as a status or navigation bar)

    Programmatically you can achieve with the following code snippet (the same can be achieved via IB too):

    override func viewDidLoad() {
      super.viewDidLoad()
    
      automaticallyAdjustsScrollViewInsets = false
      tableView.translatesAutoresizingMaskIntoConstraints = false
      NSLayoutConstraint.activate([
        tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        tableView.topAnchor.constraint(equalTo: 
           topLayoutGuide.bottomAnchor),
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
      ])
    }
    

    Note: topLayoutGuide is deprecated in iOS 11, we should use the safeAreaLayoutGuide property of UIView instead.

    0 讨论(0)
  • 2020-12-23 00:26

    I came up with the following solution, which, on navigating to the view controller for the first time, adjusts the table view's contentInset for the navigation bar's height, taking into account any padding that the top cell might have. When returning to this view controller after pushing another view controller onto the stack, I then re-adjust the contentInset to UIEdgeInsetsZero:

    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        [self adjustEdgeInsetsForTableView];
    }
    
    - (void)adjustEdgeInsetsForTableView {
        if(self.isMovingToParentViewController) {
            self.tableViewForm.contentInset = UIEdgeInsetsMake(self.navigationController.navigationBar.frame.size.height + padding, 0, 0, 0);
        } else {
            self.tableViewForm.contentInset = UIEdgeInsetsZero;
        }
    }
    
    0 讨论(0)
  • 2020-12-23 00:26

    I combined @Adam Farrell and @Tash Pemhiwa 's solutions, and finally the code below works for me:

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        [self adjustEdgeInsetsForTableView];
    }
    
    
    
    - (void)adjustEdgeInsetsForTableView
    {
        if(self.isMovingToParentViewController) {
            self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        } else {
            self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
        }
    }
    

    Hope this will help people who waste couple of hours on this weird UI behavior.

    0 讨论(0)
提交回复
热议问题