iOS 11 prefersLargeTitles not updating until scroll

前端 未结 24 2348
轻奢々
轻奢々 2021-01-31 07:25

I implemented a basic UIViewController with a UITableView that\'s wrapped in a UINavigationController. I set prefersLargeTitles to true:

override fu         


        
24条回答
  •  -上瘾入骨i
    2021-01-31 08:23

    I had the similar issue. The view is a table view. The property of prefersLargeTitles is set at viewDidLoad event. Then I set view's title in viewWillAppear event.

    override open func viewDidLoad() {
       if #available(iOS 11.0, *) {
            self.navigationController?.navigationBar.prefersLargeTitles = true
        } else {
            // Fallback on earlier versions
        }
        ...
    }
    
    override open func viewWillAppear(_ animated: Bool) {
        self.navigationItem.title = "something"
        ...
    }
    

    In my prepare segue event, I set navigation item's tile to nil so that the next view left navigation var item displays "Back" automatically.

    override func prepare(for segue: UIStoryboardSegue,
                          sender: Any?) {
        self.navigationItem.title = nil
        ...
    }
    

    The first time the table view displays large title correctly. However, if I select a row to next view and back to the table view, the navigation item's title becomes empty.

    After several hours' struggling, I finally find out that the view's title should be set in viewDidAppear event! It seems that whatever set to view's title in Will event would be reset by UIKit internally back to nil. So it has to be set in a different event.

    override func viewDidAppear(_ animated: Bool) {
       self.navigationItem.title = "something"
       ...
    }
    
    override open func viewWillAppear(_ animated: Bool) {
        // self.navigationItem.title = "something" // Remove it and set title in Did event!
        ...
    }
    

    Before I introduced this iOS 11 new feature, my app runs OK. It seems that the new feature has some changes in UIKit so that the previous version app may need some updates/changes to make it work.

提交回复
热议问题