0

I have a UIViewController as the root of a UINavigationController and I have set the preference for large titles as follows navigationController?.navigationBar.prefersLargeTitles = true

My goal is to align the left of my view to the left of the navigation title but I want to do it dynamically rather than by hard coding values. Here is the desired end result:

UINavigationBar Alignment

I try to retrieve the left or the x position of the navigation bar's title using

let navigationLeftX = navigationController!.navigationBar.layoutMargins.left

I also printed this value out and got the value 8.0

So then I thought, I should set this as the x position of the the view created and it should line up, my code in viewWillAppear:

// Create a demo view
let demoView = UIView()
view.addSubview(demoView)
demoView.backgroundColor = .orange

// Get the current left margin of the nav title
let navigationLeftX = navigationController!.navigationBar.layoutMargins.left

// Add constraints to your view
demoView.translatesAutoresizingMaskIntoConstraints = false
demoView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: navigationLeftX).isActive = true
demoView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
demoView.widthAnchor.constraint(equalToConstant: 100).isActive = true
demoView.heightAnchor.constraint(equalToConstant: 100).isActive = true

The result is close but still quite off.

UINavigationController UINavigationBar alignment with subview

If I doubled the margin x value, it seems to align properly

// Get the current left margin of the nav title
let navigationLeftX = navigationController!.navigationBar.layoutMargins.left * 2

UINavigationBar Alignment Swift

So I was just about to conclude that apart from the left margin of 8 points, there was an additional 8 points padding and logically thought, If I set the left margin of the navigation to 0, I should see an 8 points padding so I did:

navigationController!.navigationBar.layoutMargins.left = 0

But to my surprise, it was actually at the left edge of the screen without any padding:

UINavigationBar title padding margin alignment

Can anyone explain this behavior and perhaps share the best way to dynamically align a view with the navigation bar title ?

Thanks in advance

Shawn Frank
  • 4,381
  • 2
  • 19
  • 29

1 Answers1

0

A similar question was asked another user

I came across two ways which could potentially give this value:

1. Iterating through the navigation bar hierarchy

var leftMargin: CGFloat = 0.0

for view in navigationController!.navigationBar.subviews
{
    if let classToFind = NSClassFromString("_UINavigationBarLargeTitleView"),
       view.isKind(of: classToFind)
    {
        leftMargin = view.layoutMargins.left
    }
}

// Set your left margin of view to align to leftMargin variable

The two unknowns here is I am not sure if this is querying a private class and if this is fine. The second thing is, this class name could change in future iOS releases.

2. systemMinimumLayoutMargins

Another thing I came across was getting the systemminimumlayoutmargins which you could do like this:

var leftMargin = navigationController!.systemMinimumLayoutMargins.leading

// Set your left margin of view to align to leftMargin variable

This gives a value which works, however I am not sure if this was the intended usage to depict the left value of the title

I will not mark this as answered in case someone comes up with a better solution but just noted this for anyone this might help.

Shawn Frank
  • 4,381
  • 2
  • 19
  • 29