当前位置: 首页 > 面试题库 >

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

巫坚白
2023-03-14
问题内容

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

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
}

问题答案:

您可以使用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))


 类似资料:
  • 我用的是bootstrap4。我想创建一个类似android的导航栏,在最左边有一个图标条。与导航栏右侧的图像和其他图标完全一致。 我试过应用ml auto、mx auto、mr auto、向右拉等等。没有任何东西能满足我的需要。 MX-汽车是不错的小屏幕。当汉堡菜单在那里的时候,它把纳巴品牌放在中心。然而,我需要一些在常规菜单存在时有效的东西。 我已经尝试了下面,但项目打破不同的手机屏幕大小。

  • 本文向大家介绍CSS对齐导航栏,包括了CSS对齐导航栏的使用技巧和注意事项,需要的朋友参考一下 示例 水平对齐的导航(菜单)栏包含一些应该对齐的项目。容器中的第一个(左)项目没有左边距,容器中的最后一个(右)项目没有右边距。项目之间的距离是相等的,与单个项目的宽度无关。 的HTML 的CSS 笔记 的nav,ul并且li标签被选择为他们的“导航(菜单)项的列表”语义。当然也可以使用其他标签。 所述

  • 如何将导航栏项向右对齐? 我想在右边登录和注册。但是我尝试的一切似乎都不起作用。

  • 问题内容: 因此,我想在导航控制器的导航栏中的标题下方添加一个“字幕”。 到目前为止,我查找的大部分内容都希望我使用CGRect。我不太清楚这是什么,这听起来像是要我创建一个全新的视图,而这并不是我想要的。 我的问题是,是否可以使用点方法轻松添加字幕视图? 显然去年这可行,但是现在我遇到了错误,它在我的viewDidLoad中… 我尝试了这个: self.navigationController?

  • 本功能是响应大家需求,后期加上的,其实本人在公司项目或者个人项目中是不太使用该功能的。以前那些传统后台框架往往会包含此功能,由于以前的后台项目大部分都是多页面的形式,所以标签栏导航功能还是具有一定意义的,大部分都是基于 iframe 的方式实现的。 但随着时代的发展,现在的后台项目几乎都是 spa(single page web application 单页面开发),再使用以前的方案来实现标签导航

  • 我试图将导航栏项目水平居中对齐,但它们向左对齐。 有人能帮忙吗? 这是一个桌面问题-只是要明确,我有这个问题在桌面级别。 这是我目前的代码: 演示:https://jsfiddle.net/cjoasysz/