Home > OS >  How to achieve the auto-shrink SwiftUI Navigation Title with UIKit?
How to achieve the auto-shrink SwiftUI Navigation Title with UIKit?

Time:11-15

I am trying to achieve the auto-shrink large nav title like the Navigation Title in SwiftUI, but no matter what my nav title always stay shrinked and never appear in large title mode. How do i fix this? (code below) enter image description here

func setup() {
    
    table.translatesAutoresizingMaskIntoConstraints = false
    table.dataSource = self
    table.delegate = self
    table.register(UITableViewCell.self, forCellReuseIdentifier: "mycell")
    table.separatorColor = .white
    table.backgroundColor = .black
    
    let appearance = UINavigationBarAppearance()
    appearance.titleTextAttributes = [.foregroundColor: UIColor.black]
    appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.black]
    appearance.configureWithOpaqueBackground()
    
    navigationItem.title = "Todo List"
    navigationItem.scrollEdgeAppearance = appearance
    navigationItem.standardAppearance = appearance
    
    view.addSubview(table)
}

CodePudding user response:

As well as providing a configuration for large titles, you need to instruct your navigationController to use large titles

navigationController.navigationBar.prefersLargeTitles = true

The default configuration for a view controller should be to automatically use large titles if that is the preferred choice. However you can set it explicitly per view controller:

navigationItem.largeTitleDisplayMode = .always // or .automatic or .never

If the nav controller has prefersLargeTitle = false then large titles will never be displayed whatever the largeTitleDisplayMode setting.

  • Related