iOS 11 prefersLargeTitles not updating until scroll

前端 未结 24 2333
轻奢々
轻奢々 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条回答
  • 2021-01-31 08:07

    Modifying the contentInset of the tableView with top:1 will force the NavigationBar to expand and display the large titles.

    Obj-C

    -(void) viewWillAppear:(BOOL)animated {
        if (@available(iOS 11.0, *)) {
            tableView.contentInset = UIEdgeInsetsMake(1, 0, 0, 0);
        }
    }
    

    Swift

    override func viewWillAppear(_ animated: Bool) {
        if #available(iOS 11.0, *) {
            tableView.contentInset = UIEdgeInsetsMake(1, 0, 0, 0)
        }
    }
    

    Note: If you have a tableView.reloadData() in your viewWillAppear make sure to call it after editing the contentInset

    0 讨论(0)
  • 2021-01-31 08:07

    Similar issue for me with a UITableViewController added to a UIViewController. In my instance, these view controllers are themselves embedded in a UITabBarController and only the first tab displayed correctly used a large title. Other tabs required a manual scroll before the large title was displayed.

    The only thing I could get to work was adjusting the contentInset as per @pau-senabre's answer, except I the top inset wasn't helpful for me. Instead, I set the left inset and then reset it on the next runloop.

    private var isFirstAppearance = true
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        if isFirstAppearance {
            applyLargeTitlesFix()
        }
    }
    
    private func applyLargeTitlesFix() {
        let originalInset = tableViewController.tableView.contentInset
        tableViewController.tableView.contentInset = UIEdgeInsets(top: 0, left: 1, bottom: 0, right: 0)
        DispatchQueue.main.async { [weak self] in
            self?.tableViewController.tableView.contentInset = originalInset
        }
        isFirstAppearance = false
    }
    
    0 讨论(0)
  • 2021-01-31 08:09

    One more possible solution is to end refresh in your refreshHandler(). like this-

    @objc func refreshPage() {
        self.refreshControl?.endRefreshing() //End here
        self.loadTableData() //Get fresh data and reload table
    }
    
    0 讨论(0)
  • 2021-01-31 08:11

    For me the only working solution is:

    DispatchQueue.main.async { [weak self] in
        self?.navigationController?.navigationBar.sizeToFit()
    }
    

    in

    viewWillAppear()
    
    0 讨论(0)
  • 2021-01-31 08:13

    I think It does seem a little bit dummy but I effectively solved the problem with this:

    self.navigationItem.prompt = ""
    
    self.navigationItem.prompt = nil
    

    It's like navigationBar needs a sort of update in one of its elements to update the layout.

    Sometimes to update something in navigationBar I need to hide and unhide it.. That's why I think there is a best way to do it.. For the moment that's my workaround.

    0 讨论(0)
  • 2021-01-31 08:14

    I just had this same issue and, in my case, it turns out that the Storyboard structure that was working in iOS 10 with Swift 3 (and also works with iOS 11 with Swift 3) was causing the issue on iOS 11 with Swift 4.

    To elaborate:

    I had a regular UIViewController in my storyboard that I had set to a UINavigationController subclass (my hierarchy is similar to yours, with UITabBarController subclass → UINavigationController subclass → UITableViewController subclass).

    In iOS 10, this worked fine.

    In iOS 11, this also works fine when you run the existing Swift 3 app.

    However, with the Swift 4 app, running on iOS 11, I was seeing the same symptoms you described (large titles only appear when you pull/scroll the view down).

    To fix, I replaced the UIViewController-based elements in the Storyboard with actual UINavigationController instances (which contain a UINavigationBar explicitly in the Storyboard – I have a hunch this is where the crux of the issue stems from, as the UIViewController instances didn’t have that element explicitly declared within the Storyboard).

    Anyway, that fixed the issue for me.

    I’ll file radar as this looks like a Swift 4-based regression as, for me, it works both in iOS 10 with Swift 3 and in iOS 11 with Swift 3.

    0 讨论(0)
提交回复
热议问题