How do I left align the title of a navigation bar

2020-02-12 03:56发布

I've been trying the following in order to get the title of a navigation bar left aligned:

In the AppDelegate.swift file:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.


    UINavigationBar.appearance().barTintColor = UIColor.red
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:
        UIColor.white]
    return true
}

In a TableViewController.swift file:

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController?.navigationBar.topItem?.title = "Home"
    }

but nothing I find solves the problem. I also tried the following that I found on here which does not show anything:

in the AppDelegate.swift file:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.


    UINavigationBar.appearance().barTintColor = UIColor.red
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:
        UIColor.white]
let lbNavTitle = UILabel (frame: CGRect(x: 0, y: 40, width: 320, height: 40))
    lbNavTitle.center = CGPoint(x: 160, y: 285)
    lbNavTitle.textAlignment = .left
    lbNavTitle.text = "Home"
    self.navigationItem.titleView = lbNavTitle

In a TableViewController.swift file:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.navigationController?.navigationBar.topItem?.title = "Home"
    let lbNavTitle = UILabel (frame: CGRect(x: 0, y: 0, width: 320, height: 40))
    lbNavTitle.backgroundColor = UIColor.red
    lbNavTitle.textColor = UIColor.black
    lbNavTitle.numberOfLines = 0
    lbNavTitle.center = CGPoint(x: 0, y: 0)
    lbNavTitle.textAlignment = .left
    lbNavTitle.text = "Home"

    let string = NSMutableAttributedString ("Title/nSubTitle")

    self.navigationItem.titleView = lbNavTitle
}

2条回答
家丑人穷心不美
2楼-- · 2020-02-12 04:23

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))
查看更多
姐就是有狂的资本
3楼-- · 2020-02-12 04:29
let label = UILabel()
label.textColor = UIColor.white
label.text = "TCO_choose_reminder".localized;
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: label)

I init a UIBarButtonItem with the constructor that gets a UIView and set my desired label as parameter to get the following.

enter image description here

查看更多
登录 后发表回答