2017-09-26 7 views
2

ナビゲーションバーの戻るボタンのテキストの色のみを変更したいと考えています。ナビゲーションバーにbackBarButtonItemタイトルのスタイルを設定するにはどうすればよいですか?

回避策として、私はカスタムビューを作成してnavigationItem.leftBarButtonItemに割り当てることでやりたいことを並べ替えることができますが、それほど良く見えず、スワイプからポップまでの能力も失います。上記のため enter image description here

コード:

let button = UIButton(type: .system) 
    let originalImage = #imageLiteral(resourceName: "BackButton") 
    let scaledImage: UIImage = UIImage(cgImage: originalImage.cgImage!, scale: 30, orientation: originalImage.imageOrientation) 

    button.setImage(scaledImage, for: .normal) 
    button.setTitle("YourTitle", for: .normal) 
    button.sizeToFit() 
    button.setTitleColor(.brown, for: .normal) 
    button.tintColor = .blue 

    navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button) 

私も物事が

navigationController?.navigationBar.topItem.backBarButtonItem?.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red], for: .normal) 

を経由して戻るボタンの属性を設定するよう提案したがそれは上の任意の効果を持っていないようです見ますにもかかわらず、テキストの外観

print("Attributes: ", navigationController?.navigationBar.topItem?.backBarButtonItem?.titleTextAttributes(for: .normal) ?? "No attributes") 

resulti ngはAttributes: ["NSColor": UIExtendedSRGBColorSpace 1 0 0 1]です。

私はtintColorと設定できましたが、それはタイトルに加えてバックアイコンの色も変わるでしょう。

私は何をしたいのですか?方法はありますか?

答えて

0

私はそれを理解しました。前のビューコントローラでself.navigationItem.backBarButtonItemを設定することによって、戻るボタンのスタイルを設定できます。

たとえば、私の場合はTableViewControllerでした。セルをクリックすると、アプリケーションはViewControllerに移行します。 TableViewControllerで私は

public func changeColor() { 
    let barButton = UIBarButtonItem(title: "Anything", style: .plain, target: nil, action: nil) 
    barButton.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.brown], for: .normal) 

    self.navigationItem.backBarButtonItem = barButton 
} 

を持っていたし、その後ViewControllerに私は茶色への戻るボタンのタイトルの色を変更しますViewControllerでボタンを押すと、その結果

@IBAction func buttonPressed(_ sender: Any) { 
    let vc = self.navigationController?.viewControllers[0] as! TableViewController 
    vc.changeColor() 
    self.title = "hello very long title asdfasdfasfdasdfasdfasdfasdfasdf" 
} 

を持っていました。

0

私はあなたを正しく理解しているかどうかはわかりません。しかし、以下のコードを試してみてください。これはアプリのすべてのバーボタンアイテムに適用されます。このコードは、アプリのライフサイクルを1回だけ呼び出す場所に配置します。同様にapplication: didFinishLaunchingWithOptions:

let attribs = [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: UIFont.boldSystemFont(ofSize: 18)] 
UIBarButtonItem.appearance().setTitleTextAttributes(attribs, for: .normal) 
+0

あなたのソリューションは、アプリ全体のバーボタンアイテムの色を変更します。 1つのビューコントローラーで戻るボタンの色を変更したかっただけです。私の答えを見てください。とにかくありがとうございます。 – TWOF

関連する問題