Can't change UINavigationBar prompt color
It seems like you're right about this one. You need to use UIAppearance
to style the prompt text on iOS 11.
I've filed radar #34758558 that the titleTextAttributes
property just stopped working for prompt in iOS 11.
The good news is that there are a couple of workarounds, which we can uncover by using Xcode's view hierarchy debugger:
// 1. This works, but potentially changes *all* labels in the navigation bar.
// If you want this, it works.
UILabel.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).textColor = UIColor.white
The prompt is just a UILabel. If we use UIAppearance's whenContainedInInstancesOf:
, we can pretty easily update the color the way we want.
If you look closely, you'll notice that there's also a wrapper view on the UILabel. It has its own class that might respond to UIAppearance...
// 2. This is a more precise workaround but it requires using a private class.
if let promptClass = NSClassFromString("_UINavigationBarModernPromptView") as? UIAppearanceContainer.Type
{
UILabel.appearance(whenContainedInInstancesOf: [promptClass]).textColor = UIColor.white
}
I'd advise sticking to the more general solution, since it doesn't use private API. (App review, etc.) Check out what you get with either of these two solutions:
Can't change UINavigationBar prompt color and font
Try this code in viewWillAppear(_ animated: Bool)
:
for view in self.navigationController?.navigationBar.subviews ?? [] {
let subviews = view.subviews
if subviews.count > 0, let label = subviews[0] as? UILabel {
label.textColor = UIColor.white
label.font = UIFont.systemFont(ofSize: 30)
}
}
How to change background color of UINavigationItem?
You can change it through code...
For Objective-C:
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
Write Above line in viewDidLoad method.
For Swift:
self.navigationController?.navigationBar.barStyle = UIBarStyle.BlackTranslucent
self.navigationController?.navigationBar.barTintColor = UIColor.redColor();
OR
self.navigationController!.navigationBar .setBackgroundImage(UIImage .new(), forBarMetrics: UIBarMetrics.Default)
self.navigationController!.navigationBar.shadowImage = UIImage .new();
self.navigationController!.navigationBar.translucent = true;
self.navigationController!.navigationBar.backgroundColor = UIColor.redColor();
You can change color on your own choice.
To change the bar Text...
navigationController.navigationBar.titleTextAttributes = [UITextAttributeTextColor: UIColor.blueColor()]
See the Link....
Here
See the above image... you like output like this screen right...!!!
Why can't I set a custom color-tint for the UINavigationBar?
I can be such an idiot ... for months now, I assumed the values ranged from 0..255 (i.e., HTML color values), but they are in the range [0.0 .. 1.0]. Now that I set my 0..255 value and normalize it to 0..1 it works as expected. I wrote a simple color-slider application and when I was debugging it, is when I realized my mistake.
Changing navigation items color through the entire app?
UINavigationItem
is not a view and it doesn't have a color.
You instead want to change the UIBarButtonItem
tint color.
Using the UIAppearance
proxy you can do
[[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];
This will change the tintColor
of each UIBarButtonItem
in the application.
You can use the same strategy to change the UINavigationBar
barTintColor
and titleTextAttributes
:
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; // this will change the back button tint
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
Unfortunately there's no way of changing the translucent
property using the proxy, so you will have to do it on each bar.
How to change text color of status bar prompt?
You can add a UILabel to your navigation and set it's color
-(void) viewDidLoad {
UILabel *theLabel = [ [UILabel alloc ] initWithFrame:CGRectMake((self.bounds.size.width / 2), 0.0, 150.0, 43.0) ];
theLabel.textAlignment = UITextAlignmentCenter;
theLabel.textColor = [UIColor whiteColor];
theLabel.backgroundColor = [UIColor blackColor];
theLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(36.0)];
UIView *myLabel = (make a view and add "theLabel" to it)
UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] initWithCutomView:myLabel];
[self.navigationController.navigationItem setRightBarButtonItem:myBarButtonItem animated:NO]
}
Related Topics
Displaying Instances of View Controllers Within Xcode Without Disturbing the Current Hierarchy
Completely Unrelated Error Using Coredata
iOS - Setobject VS. Synchronize
Multiple Image Pickers in One Controller
Swift Good Coding Practice: If Statement with Optional Type Bool
How to Work with Udp Sockets in iOS, Swift
Creating a Custom UI View Class to Use as a Map Annotation
Firebase Authentication Error in Other Languages
Screen Recording When My iOS App Is in Background with Replaykit
Coretelephony Esim Functions Not Working on Device
Swift Link Image from Parse Array Using Segues to Secondviewcontroller
Google Signin Cocoapods Deprecated
iOS PDFkit Displaymode = Singlepage Only Shows the First Page of the PDF
Swift3 Random Extension Method
@Fetchrequest + Case-Insensitive Sorting - Swiftui & Coredata
Avcapturevideopreviewlayer Add Overlays and Capture Photo in iOS