Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Swift method for indenting the large title of a navigation bar in line with cellLayoutMarginsFollowReadableWidth?

Currently, on iPad, my UINavigationBar large title looks strange because it does not indent to where my UITableView cells are. This is because my table view and cells follow readable width. Is there a way of indenting this title or is the only piece of advice to align the cells with the title (make them not follow readable width). I would really appreciate anyone who took the time to respond.

like image 475
Ben Avatar asked Sep 17 '25 02:09

Ben


1 Answers

When you first load your view controller, you should retrieve the minX coordinate of the current readableContentGuide and set that value to the left layoutMargin value. As so:

let leftMargin = self.view.readableContentGuide.layoutFrame.minX    
self.navigationController?.navigationBar.layoutMargins.left = leftMargin

Then, you'll also want to make sure your layout adjusts dynamically so that if the user rotates their screen (or on an iPad, is using a variable window size) the title will remain aligned:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)

    coordinator.animate(alongsideTransition: { _ in
        let leftMargin = view.readableContentGuide.layoutFrame.minX    
        navigationController?.navigationBar.layoutMargins.left = leftMargin
    }, completion: nil)
}
like image 132
swillsea Avatar answered Sep 19 '25 03:09

swillsea