Sean
Sean

Reputation: 1

Navbar title become small when scroll down and go back from a UITableViewController

The main navigation bar become small when go back from a table view with scrolling. Can anyone show me the correct way to implement large title?

Video Sample https://i.imgur.com/zoATpja.gif

ViewController

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController?.navigationBar.prefersLargeTitles = true
}

DestinationViewController

let reuseIdentifier = "cell"

let array = ["Test 1","Test 2","Test 3"]

override func viewDidLoad() {
    self.title = "TableView"
    self.navigationItem.largeTitleDisplayMode = .never

    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
    self.tableView.delegate = self
    self.tableView.dataSource = self
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return array.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)

    cell.textLabel?.text = array[indexPath.row]

    return cell
}

Upvotes: 0

Views: 1548

Answers (3)

nayem
nayem

Reputation: 7605

Well, after tinkering with the issue, I've come to a conclusion that:

This is probably a bug when using Large titles in combination with UIViewController.

Then I found in one of your comments: but iPhone Settings and App Store has the similar animation...

But the thing is that both of the reference apps uses UITableViewController subclass for the source (of the segue) view controller when the navigation happens. So I tried similar approach and YES my doubt is correct. You can find the reference project here where the animation issue isn't present.


So, you might want to change your source view controller to be a subclass of UITableViewController until there is an official fix for the issue.

Upvotes: 0

Abhishek Jadhav
Abhishek Jadhav

Reputation: 706

This helps you!!

Call this method from viewDidLoad()

/**
In Swift 4.2
*/
 func setupNavBar() {

            self.title = "titleName"
            self.navigationController?.navigationBar.prefersLargeTitles = true
            self.navigationController?.navigationItem.largeTitleDisplayMode = .always        
     }

In DestinationView Controller put this two lines in ViewDidLoad method.

self.navigationItem.largeTitleDisplayMode = .never
self.navigationController?.navigationBar.prefersLargeTitles = false

Upvotes: 1

Ievgen Leichenko
Ievgen Leichenko

Reputation: 1317

You need to call this

self.navigationController?.navigationBar.prefersLargeTitles = true

and this

self.navigationItem.largeTitleDisplayMode = .never

in viewWillAppear()

Upvotes: 0

Related Questions