Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How iOS UITableView under NavigationBar?

I have set

NavigationController.NavigationBar.Translucent = true;

Then add table and set frame to RootView Frame, and:

    public override void ViewDidLayoutSubviews()
        {
            base.ViewDidLayoutSubviews();
            float y = this.TopLayoutGuide.Length;
            table.ContentInset = new UIEdgeInsets (y, 0, 0, 0);
        }

But, I Have table Scroll Bar under NavigationBar (I use monotouch):

enter image description here

like image 345
Dmitriy Avatar asked Jan 06 '14 09:01

Dmitriy


2 Answers

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 on iOS 11, we should use the safeAreaLayoutGuide property of UIView instead.

like image 64
Madiyar Avatar answered Sep 18 '22 11:09

Madiyar


Try this:

    if([self respondsToSelector:@selector(edgesForExtendedLayout)])
    {
       self.edgesForExtendedLayout = UIRectEdgeNone;
       self.automaticallyAdjustsScrollViewInsets = NO;
    }
like image 43
HoanNguyen Avatar answered Sep 21 '22 11:09

HoanNguyen