Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a title to the left side of the navigation bar

Is it possible to add a title to the left side of the navigation bar? I know how I can add a title in the center but when I try to add one to the left side I see nothing. This is the code I have:

self.navigationItem.leftBarButtonItem?.title = "Elapsed Time: 0:00"

Thank you for the help.

like image 694
Brejuro Avatar asked Apr 29 '15 16:04

Brejuro


People also ask

How do I change the color of my navigation bar title?

Changing the text color The text color of the navigation bar can be changed using two inbuilt classes: navbar-light: This class will set the color of the text to dark. This is used when using a light background color.

How do I change the navigation bar title color in SwiftUI?

To change a navigation bar color in SwiftUI, you apply toolbarBackground modifier to the content view of NavigationStack . NavigationView is deprecated in iOS 16. toolbarBackground accepts two parameters. ShapeStyle : The style to display as the background of the bar.


2 Answers

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.

like image 106
Vasyl Khmil Avatar answered Oct 05 '22 03:10

Vasyl Khmil


    navigationController?.navigationBar.isTranslucent = false
    
    let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width - 32, height: view.frame.height))
    titleLabel.text = "  Home"
    titleLabel.textColor = UIColor.white
    titleLabel.font = UIFont.systemFont(ofSize: 20)
    navigationItem.titleView = titleLabel
like image 40
Yestay Muratov Avatar answered Oct 05 '22 02:10

Yestay Muratov