maxedison
maxedison

Reputation: 17573

iOS: Confusion over Interface Builder

There are times when I set things in Interface builder and they show up as I expect in my app. Other times, they show up differently or not at all. And I can't seem to get a good handle on why this is the case.

For example, I am presenting a Navigation Controller in a modal view. But in Interface Builder, when I try to set the title of the Navigation Bar, it gets ignored. I understand that the navigation bar title takes its name from the title of the view its displaying, so you actually need to set the title on that subview rather than on the navigation bar. That makes plenty of sense when considering code only, but the fact that IB allows you to seemingly set the title is very confusing. Why does it let you? Just so that it looks "right" in IB even if that doesn't reflect the true state of the app?

A similar example is adding a UIBarButtonItem to the navigation bar. Again, in IB I can add it, set its title, style, etc. But it doesn't show up. I have to set the .navigationItem.leftBarButtonItem property on the root view controller that the navigation controller is initialized with.

So as far as I can tell, neither of the two things above (title & button) are achievable through IB, yet IB sure makes it seem like they are. And in the end, this makes it meaningless to even have a XIB file for the Navigation Controller.

Am I missing something?

Upvotes: 2

Views: 828

Answers (1)

August Lilleaas
August Lilleaas

Reputation: 54603

When a xib has "Navigation Bar" set as "Top Bar" under "Simulated Metrics", the title can not be set, and buttons can not be added. Did you manually add a navigation bar to the xib? If you did, that's why IB lets you set the title and add bar button items. You shouldn't manually add navigation bars just because you expect one to be added by a navigation controller at some point. You should use the simulated metrics.

Upvotes: 5

Related Questions