Philip Stratford Philip Stratford - 1 year ago 74
iOS Question

Display View Title on Next Line if Back Button Text Too Long

This is my first day trying to write an iOS app, having followed the tutorials on the Apple site and a few others, so be gentle. It's actually going pretty well so far!

My main view is a list of company contacts. Tapping on a contact segues to a view showing that contact's details. I've embedded my views in a Navigation view, and the navigation all works fine. On the contact details view, the view title (

) is being set to the person's name in the viewDidLoad function.

The problem I've got is that the text of the 'back' navigation button in the title bar on the contact details view is being derived from the title of the main view, as expected, but that text is long enough to push the title of the contact detail view - the person's name - to the right, like this:screenshot

I was wondering if this is the expected and correct behaviour for an iOS app or if I should attempt to have the contact detail view's title displayed on a different line to the back navigation button, and, if so, how I would do that.

I know that I can override the text of the back navigation button (indeed, it's automatically set to "Back" if my main controller's title is set to something a bit longer, interestingly), but that's not going to solve the problem if a person has a particularly long name!

Answer Source

What you're seeing is indeed the correct/expected behavior.

If you don't like that back button text, you can always change or remove it:

    let backButtonItem: UIBarButtonItem = UIBarButtonItem()
    backButtonItem.title = "" // Set your custom text here
    navigationItem.backBarButtonItem = backButtonItem

If you want to do a multi-line title you'll have to customize it by either creating your own UINavigationBar subclass or by setting a custom titleView on your UINavigationItem and setting a custom view in which you render multiple lines.