2017-09-27 186 views
4

我想在iOS 11中使用Swift使用新的大標題系統。當標題變得太長時(請參閱圖像示例),它會添加...而不是換行或縮小文本大小。我如何添加換行符?如何在iOS 11中打破較長的大標題?

Example image with long title

下面是一些代碼,我使用設置標題:

self.navigationController?.navigationBar.prefersLargeTitles = true 
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: MyGlobalVariable.themeMainColor] 
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 22)] 
navigationItem.title = "Search by Name" 
+0

我敢肯定,只有一行被允許在標題。我沒有看到兩行標題。 – CoderFrom94

+0

從我讀過的所有內容來看,你可能是對的。我認爲我需要從另一個角度來處理我的問題。謝謝! – Coltuxumab

+0

很酷,我昨天證實,只有一個像標題可能。對不起,我沒有更新它。趕上了工作。 :) – CoderFrom94

回答

1

試試這個:

for navItem in (self.navigationController?.navigationBar.subviews)! { 
    for itemSubView in navItem.subviews { 
     if let largeLabel = itemSubView as? UILabel { 
      largeLabel.text = self.title 
      largeLabel.numberOfLines = 0 
      largeLabel.lineBreakMode = .byWordWrapping 
     } 
    } 
} 

它爲我工作。

enter image description here