UINavigationBar overlaps UITableView when programm

2019-01-28 09:11发布

I have a UINavigationController which contains a UITableViewController. This navigation controller pushes other UITableViewControllers around and eventually these table view controllers will have a prompt.

The problem is when I set this prompt programmatically it overlaps the content of the table view underneath it.

enter image description here

(A search bar is being hidden by the navigation bar)

I was looking around in SO and found this answer. I tried the suggestion there in two different ways in the affected view controller but nothing changed:

override func viewDidLoad() {
    super.viewDidLoad()
    self.edgesForExtendedLayout = .None;
    self.extendedLayoutIncludesOpaqueBars = false;
    self.navigationItem.title = NSLocalizedString("Add Anime or Manga", comment: "")
    self.navigationItem.prompt = NSLocalizedString("Search media belonging to this series.",  comment: "")
}

-

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.title = NSLocalizedString("Add Anime or Manga", comment: "")
    self.navigationItem.prompt = NSLocalizedString("Search media belonging to this series.",  comment: "")
    self.edgesForExtendedLayout = .None;
    self.extendedLayoutIncludesOpaqueBars = false;
}

A comment in that same answer linked to this Apple guide on preventing views from overlapping each other. The problem is UITableViewController doesn't appear to have top/bottom layout guides so I can't create a constraint (another SO answer says having said layouts in table view controllers is irrelevant).

As such I have exhausted all my options.

3条回答
ら.Afraid
2楼-- · 2019-01-28 09:43

I have tried to reproduce your problem and it seems that when not all the viewControllers have a prompt the navigationBar is somehow not resizing properly.

It seems you need to somehow trigger the layouting for the UINavigationController. The only way I could make it work properly was by adding this in viewWillAppear:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self.navigationController setNavigationBarHidden:YES];
    [self.navigationController setNavigationBarHidden:NO];

}

Maybe this prompt is meant to be used consistently across the entire application (meaning having one for all viewControllers or none of them), that's why the UINavigationController does not layout it's subviews when it changes.

Hope this works for you too.

查看更多
smile是对你的礼貌
3楼-- · 2019-01-28 09:48

Select your TableViewController from document outline and change the value to translucent navigation bar of top bar in attributes inspector. Be sure that you will not select uitableview you should select your your table view controller(aka File's Owner) from document outline.

查看更多
神经病院院长
4楼-- · 2019-01-28 09:50

You have to set prompt only if view did appear, then it works:

    override func viewDidAppear(_ animated: Bool) {
         navigationItem.prompt = "your prompt here"
    }
查看更多
登录 后发表回答