2017-06-12 68 views
1

我一直在努力,以获得一个导航栏的标题下面的左对齐:我该如何左对齐Xcode中导航栏的标题?

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 
} 
+0

@DashAndRest感谢您的建议。你能否解释为什么你建议格式化文件名,以后我怎么做? –

+0

为了吸引读者的注意力,标记重要的地方总是很好的。 – D4ttatraya

回答

3

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

谢谢。这工作。该行:self.navigationController?.navigationItem.title = label.text需要添加以确保您获得所需的结果。 –

+0

如果我通过说'无法修改由控制器管理的UINavigationBar约束来做这个应用程序崩溃。我尝试过ViewDidLoad,ViewWillAppear&viewDidLayoutSubviews – Neha

+0

@Neha可能是很多事情。我会建议问你自己的问题,然后允许你添加你的代码的细节等。 –