2

I can't read the foreground color defined into the titleTextAttributes property of a UINavigationBar instance.

Here is my attempt:

label.textColor = navigationBarInstance.titleTextAttributes[NSForegroundColorAttributeName] as! UIColor

The titleTextAttributes is defined as var titleTextAttributes: [NSObject : AnyObject]? so one of the error that I get is that I cannot subscript that properties using a String (NSForegroundColorAttributesName). The second error that I receive is that I cannot a sign a value of type UIColor to a value of type UIColor!.

I'm really confused! I just want to get the color of the navigationBar title and copy it into a label.textColor property.

MatterGoal
  • 16,038
  • 19
  • 109
  • 186
  • Possible duplicate of http://stackoverflow.com/questions/10430298/how-to-set-font-color-of-the-title-in-uinavigationbar-using-ios5-appearance-ap – Blake Merryman May 13 '15 at 17:24

1 Answers1

-1

Swift 4.2

let navigationBarForegroundColor = navigationBar.titleTextAttributes![NSAttributedString.Key.foregroundColor] as! UIColor
hstdt
  • 5,652
  • 2
  • 34
  • 34
  • 4
    This answer has been flagged as potentially low-quality. Please elaborate a little more on why this works so that future viewers and new developers understand why your answer is a valid solution. – App Dev Guy Jul 24 '19 at 07:07