gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 05:46:49 28 4
gpt4 key购买 nike

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

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
}

TableViewController.swift 文件中:

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

但我没有找到解决问题的办法。我还尝试了我在此处找到的以下内容,但没有显示任何内容:

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

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
}

最佳答案

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

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

enter image description here

编辑:

如果您不想移除后退按钮。设置以下标志。

self.navigationItem.leftItemsSupplementBackButton = true

请记住,标签 + 后退按钮(带标题)看起来不太酷。在这种情况下,我将默认后退按钮替换为箭头 Assets 。

关于swift - 如何在 Xcode 中左对齐导航栏的标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44506238/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com