How to Left Align the Title of a Navigation Bar in Xcode

How do I left align the title of a navigation bar in Xcode?

You can use the navigationItems titleView to add a UILabel with left alignment and then set its frame using auto layout like this:

let label = UILabel()
label.text = "Title Label"
label.textAlignment = .left
self.navigationItem.titleView = label
label.translatesAutoresizingMaskIntoConstraints = false
label.superview?.addConstraint(NSLayoutConstraint(item: label, attribute: .centerX, relatedBy: .equal, toItem: label.superview, attribute: .centerX, multiplier: 1, constant: 0))
label.superview?.addConstraint(NSLayoutConstraint(item: label, attribute: .width, relatedBy: .equal, toItem: label.superview, attribute: .width, multiplier: 1, constant: 0))
label.superview?.addConstraint(NSLayoutConstraint(item: label, attribute: .centerY, relatedBy: .equal, toItem: label.superview, attribute: .centerY, multiplier: 1, constant: 0))
label.superview?.addConstraint(NSLayoutConstraint(item: label, attribute: .height, relatedBy: .equal, toItem: label.superview, attribute: .height, multiplier: 1, constant: 0))

How to create a left aligned title with prefersLargeTitles in navigationBar?

Here's a link which can help you to find a way out as you want : Change title according to navigation bar.

Some basic things would like to add is you can set navigation bar title to empty string or something else as you want and make your custom view visible if the bar is collapsed other then that make it invisible. You can check in the link how to observe if navigation bar is collapsed or not.

One more important thing is you should remove observers if you are switching to another screen or it can cause memory leaks.

Navigation Bar top item Title align left

set your navigationController?.navigationBar.isTranslucent = false

        let leftInsect = 30
let leftLabel = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width - leftInsect, height: view.frame.height))
leftLabel.text = " XXX"
leftLabel.textColor = UIColor.blue
leftLabel.font = UIFont.systemFont(ofSize: 21)
navigationItem.titleView = leftLabel

Adjust leftInsect according your need

Change navigation title position from left to right

I assume this is what you are looking for. But consider this is applied by iOS automatically dependend on your language settings.

NavigationView {
Color.init("midnight blue")
.edgesIgnoringSafeArea(.vertical)

.navigationBarTitle("המרות מטבע")
.navigationBarItems(trailing:
Button(action: {
print("button pressed")

}) {
Image(systemName: "plus")
.foregroundColor(Color.orange)
})
}.environment(\.layoutDirection, .rightToLeft)

set the alignment of title of navigation bar

You don't have to use UILabel. You can directly use this code:

[self.navigationItem setTitle:@"Your Title"];

and you're good to go! This code will automatically lay its self to the center of the navigation bar.

Or if you actually have an instance of UINavigationController, use:

[self setTitle:@"Your Title"];

Navigation bar titleView alignment

The thing is, navigation titleView always try to center your view. The reason that the title you see off-center in a smaller screen is that, the width of your custom titleView has gone beyond the maxmium width of titleView.

If you set the value to 160, you will see it centered for the second situation.

General Solution

If you want to adapt title width to different devices, there are a few approaches that may help you solve the problem.

  1. Set different width based on different screen width. For example, when screen width is 320, set it no more than 160. If larger than 320, assign value based on different screen size. This is not an elegant solution, but pretty much straightforward without banging your head into setting and trying complicated constraints.

  2. Calculate the actual width of titleView. The width of titleView is determined by navigation bar width minus left and right barbuttonitem. Something like

TitleViewWidth = Navigation width - leftbarbuttonitem.width +/- rightbarbuttonitem.width - someConstant

You need a little bit experiment for finding the appropriate constant that fits this formula.


  1. Create a xib file for this titleView, set up constraints more flexibly than claim a fixed width. Load view from xib and then assign to titleView. This may cost longer time to tune it to work.

Edit

I think of another way that may be easier for you if you are new to this.

In the storyboard, find and drop a view to this view controller's navigation bar titleView area.

Expand however long you want the view to be.

Drop two labels to that view, and set the leading and trailing and height and vertical constraints.

How it looks on storyboard

storyboard image

How it runs on 4S

4S running

Navigation bar title alignment

  // here to create a UILabel

UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.numberOfLines = 1;
// set your custom font for title

NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"ORDER DETAILS"];
[string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0] range:NSMakeRange(0,5)];

// set line spacing
NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
[paragrahStyle setLineSpacing:6];
[paragrahStyle setAlignment:NSTextAlignmentCenter];
[string addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [string length])];

label.attributedText = string;
[label sizeToFit];
self.navigationItem.titleView = label;

copied from :Navigationbar title alignment issue

or else the better idea is made the title as an UiImage and u can use that image as Navigation bar title. Use a properly sized 44x44 pt image with 2x and 3x versions.

UIImageView* titleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Orderpng.png"]];
imageView.contentMode = UIViewContentModeScaleAspectFit;

UIView* titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
imageView.frame = titleView.bounds;
[titleView addSubview:imageView];

self.navigationItem.titleView = titleView;

enjoy coding :)

Adding a logo in Navigation Bar that is left aligned

You can assign an array of items to the leftBarButtonItems property of the navigationBar. You should add an array because there is usually no space between the left side and the logo, and this doesn't look great. Here is an example:

func setUpUI() {
let logoImage = UIImage.init(named: "logoImage")
let logoImageView = UIImageView.init(image: logoImage)
logoImageView.frame = CGRectMake(-40, 0, 150, 25)
logoImageView.contentMode = .ScaleAspectFit
let imageItem = UIBarButtonItem.init(customView: logoImageView)
let negativeSpacer = UIBarButtonItem.init(barButtonSystemItem: .FixedSpace, target: nil, action: nil)
negativeSpacer.width = -25
navigationItem.leftBarButtonItems = [negativeSpacer, imageItem]
}

The negative spacer to the left of it will push it over a bit, and you can adjust it from there.

Adding a title to the left side of the navigation bar

Try this code:

let leftItem = UIBarButtonItem(title: "Title",
style: UIBarButtonItemStyle.plain,
target: nil,
action: nil)
leftItem.isEnabled = false
self.navigationItem.leftBarButtonItem = leftItem

More powerful solution of this problem:

let longTitleLabel = UILabel()
longTitleLabel.text = "Long long long long long long title"
longTitleLabel.font = ................
longTitleLabel.sizeToFit()

let leftItem = UIBarButtonItem(customView: longTitleLabel)
self.navigationItem.leftBarButtonItem = leftItem

Now you just can edit label as you want. You can use another font or text color.



Related Topics



Leave a reply



Submit