2017-06-12 86 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可能是很多事情。我會建議問你自己的問題,然後允許你添加你的代碼的細節等。 –