lewis
lewis

Reputation: 3202

iOS Navigation Bar Prefers Large Titles Scroll Behaviour

In iOS 11 the system apps all compress the navigation bar as you scroll down if you enable prefersLargeTitles:

enter image description here

I can't figure out how to implement this in my own apps though, the bar stays the same by default:

enter image description here

The only thing I can see is Hide Bars On Swipe, but that hides the whole bar rather than compressing it:

enter image description here

This is just an empty project created in Xcode 9 beta and with a new storyboard added.

What do I need to do to get the same behaviour as the system apps?

Upvotes: 6

Views: 9162

Answers (4)

Ponyboy
Ponyboy

Reputation: 985

It seems like this issue is happening to people for different reasons. None of the above answers helped me, but here's what DID work...

I deconstructed my app to find the cause, which was the view hierarchy in the storyboard. It appears that the UITableView view HAS to the the first view in your view controller. I had a UITableView with two UIImageViews behind it and that's what was causing the issue. Once I removed those UIImageViews everything worked correctly.

My fix: I ended up creating a UIView in code, adding my two image views to that, THEN adding that UIView to the UITableview.backgroundView.

Hope this helps someone.

Upvotes: 4

Sai Sandeep
Sai Sandeep

Reputation: 149

if #available(iOS 11.0, *) {
    navigationController?.navigationBar.prefersLargeTitles = true
    navigationController?.navigationBar.topItem?.title = "Hello"
    navigationController?.navigationItem.largeTitleDisplayMode = .automatic

    let attributes = [
        NSAttributedStringKey.foregroundColor : UIColor.red,
        ]

    navigationController?.navigationBar.largeTitleTextAttributes = attributes
} else {
    // Fallback on earlier versions
}

http://iosrevisited.blogspot.in/2017/09/navigation-bar-with-large-titles-and.html

Upvotes: 1

Rafael Bugajewski
Rafael Bugajewski

Reputation: 1712

If you have to target older iOS versions, you’ll also have to wrap the assignment in an availability check:

if #available(iOS 11, *) {
    self.navigationController?.navigationBar.prefersLargeTitles = true
}

Upvotes: 1

Claes
Claes

Reputation: 236

Don't set anything regarding Large Titles in Interface Builder / Storyboard, only in code. That worked for me.

So in the navigation bar in storyboards, Prefers Large Titles unchecked.

In your view controller:

self.navigationController?.navigationBar.prefersLargeTitles = true

Upvotes: 13

Related Questions