如何在Xcode中左对齐导航栏的标题?

Sha*_*rde 10 xcode uinavigationbar swift

我一直在尝试以下方法,以使导航栏的标题保持左对齐:

AppDelegate.swift文件中:

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
}
Run Code Online (Sandbox Code Playgroud)

TableViewController.swift文件中:

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

但我找不到任何解决问题的方法.我还尝试了以下我在这里找不到的内容:

AppDelegate.swift文件中:

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
Run Code Online (Sandbox Code Playgroud)

TableViewController.swift文件中:

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
}
Run Code Online (Sandbox Code Playgroud)

Ilk*_*aci 18

let label = UILabel()
label.textColor = UIColor.white
label.text = "TCO_choose_reminder".localized;
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: label)
Run Code Online (Sandbox Code Playgroud)

我使用获取UIView的构造函数初始化一个UIBarButtonItem,并将我想要的标签设置为参数以获得以下内容.

在此输入图像描述

  • @BrunoMuniz 你期望什么?您想将标题左对齐并在其旁边有一个后退按钮吗? (2认同)

Uph*_*uth 8

您可以使用navigationItems titleView添加左对齐的UILabel,然后使用自动布局设置其框架,如下所示:

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))
Run Code Online (Sandbox Code Playgroud)

  • 这对我不起作用,https://stackoverflow.com/a/42430844/2764966对我有用。 (2认同)

Chu*_*ZHB 5

好吧,如果您想使用大标题,那么默认情况下它是左对齐的。

对于整个导航层次结构。

navigationController?.navigationBar.prefersLargeTitles = true
Run Code Online (Sandbox Code Playgroud)

对于 1 个视图控制器

navigationItem.largeTitleDisplayMode = .always
Run Code Online (Sandbox Code Playgroud)


Les*_*ary 5

这对我有用(iOS 13+):

    let offset = UIOffset(horizontal: -CGFloat.greatestFiniteMagnitude, vertical: 0)
    navigationController?.navigationBar.standardAppearance.titlePositionAdjustment = offset
    navigationController?.navigationBar.scrollEdgeAppearance?.titlePositionAdjustment = offset
    navigationController?.navigationBar.compactAppearance?.titlePositionAdjustment = offset
Run Code Online (Sandbox Code Playgroud)