I have a been looking around to change the tint of my UINavigationBar on iOS 5.1.
I have a UITabBarController with two UINavigationControllers attached, the following code is placed in the custom UINavigationController:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
}
...but this does not have any effect.
Thanks in advance.
If you have subclassed UINavigationController (and not having a UIViewController inside a UINavigationController which is most common) then you should do this instead:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationBar setTintColor:[UIColor blackColor]];
}
because self is already a navigation controller. Otherwise your code you would have worked just fine (ie You had a UIViewController or one of its subclasses and you've embedded it via code or via Interface Builder into a UINavigationController)
try this
(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
}
Related
QLPreviewController * preview = [[QLPreviewController alloc] init];
preview.dataSource = self;
preview.currentPreviewItemIndex = sender.tag;
preview.editing= YES;
[self presentModalViewController:preview animated:YES];
[preview release];
These two lines does not work for me. so be careful before writing these lines.
[preview.tabBarController.tabBar setTintColor:[UIColor blackColor]];
[preview navigationController].navigationBar setTintColor: [UIColor blackColor]];
Since iOS5 you can theme controls based on instance, globally or when contained by specific container classes. Since iOS6 the former method of subclassing QLPreviewController to set the tintColor of the UINavigationBar stopped working.
Consider one of the following as an example of a workaround that is compatible with iOS5 and iOS6:
Any UINavigationBar contained within a QLPreviewController:
[[UINavigationBar appearanceWhenContainedIn:[QLPreviewController class], nil]
setTintColor:[UIColor blackColor]];
or globally set the tintColor of all UINavigationBar instances within your app with:
[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
This same strategy works with the UITabBarController.
set style of UINavigationController with this line..
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
and for change the color of TabBar just Add the below code in viewWillAppear of your class
CGRect frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 48);
UIView *v = [[UIView alloc] initWithFrame:frame];
[v setBackgroundColor:[UIColor colorWithRed:0.1 green:0.2 blue:0.6 alpha:0.8]];
[v setAlpha:0.5];
[[self.tabBarController tabBar] insertSubview:v atIndex:0];
[v release];
If you want to change the tintColor of the navigationBar you can push your QLPreviewController instead present it modally:
//i assume that you already have a navigationController
[[self navigationController] pushViewController:previewer animated:YES];
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
For the bottom bar i think that is a UIToolbar not a UITabBar, probably you cant change the color (i dont know), but surely you can't call preview.tabBarController.tabBar.
I found a solution , though it is not the correct way but it works:
Make subclass of QLPreviewController
MyQLPreviewController.h
#interface MyQLPreviewController : QLPreviewController
#end
and in .m of that new subclass, copy the following code
#implementation MyQLPreviewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIToolbar *toolbar = [self getToolBarFromView:self.view]; //NOTE: Not the correct apperoach! could not think better solution, as iOS does not allow to access the toolbar properties in QLPreviewController
toolbar.barTintColor = [UIColor redColor];
}
- (UIToolbar *)getToolBarFromView:(UIView *)view
{
for (UIView *subView in view.subviews)
{
if ([subView isKindOfClass:[UIToolbar class]])
{
return (UIToolbar *)subView;
}
else
{
UIToolbar *toolBar = [self getToolBarFromView:subView];
if (toolBar)
{
return toolBar;
}
}
}
return nil;
}
I'm not using a navigation controller, but I want to create a bar at the top of my UIViewController that resembles a UINavigationBar with the style of UIBarStyleBlack.
How can I do something like that?
Thanks!
iOS, xcode 4.3.3, ARC
you should following this:
typically navigationBar height is 44.0f;
- (void)viewDidLoad
{
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIApplication sharedApplication].statusBarFrame.size.width, "your_want_height")];
navBar.barStyle = UIBarStyleBlack;
[[self view] addSubview:navBar];
[super viewDidLoad];
}
I keep reading to change the default color on a navigationbar i just need to update the first method in the appdelegate to this
self.window.rootViewController.navigationController.navigationBar.tintColor = [UIColor whiteColor];
but it doesn't seem to work, so I tried setting it in the viewDidLoad method of the firstview also:
self.parentViewController.navigationController.navigationBar.tintColor = [UIColor whiteColor];
This didn't work either. How can I change this?
Don't use self.parentViewController, but self:
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
When iphone 5 come we have to set both device type. So use this
if([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
//iOS 5 new UINavigationBar custom background
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navbg_ForiPhone5_Imagename.png"] forBarMetrics: UIBarMetricsDefault];
} else {
[self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"navbg_ForOtherIphone_Imagename.png"]] atIndex:0];
}
In IOS7 you can try this:
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
You can follow these steps:
I created a new UINavigationController for example UIDemoNavController resulting in:
- (void)viewDidLoad{
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
[super viewDidLoad];
}
This is the full demo class:
#import "UIDemoNavController.h"
#interface UIDemoNavController()
#end
#implementation UIDemoNavController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {}
return self;
}
- (void)viewDidLoad{
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
}
#end
Try self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
UIViewController class has a property navigationController, which returns the navigation controller its embedded in no matter how deep, otherwise it returns nil.
I'd like to change the background color of an UINvaigationBar and I used tintColor property but it's not changing the color. You can checkout the code below.
In my Appdelegate I create a UITabBarController
tabbar = [[UITabBarController alloc] init];
Then I create a UINavigationController and attach my UITableViewController to it
UINavigationController *myNavigation = [[UINavigationController alloc] initWithRootViewController:myViewController];
Then attached the UINavigationControllers to my tabbar like this
[tabbar setViewControllers:viewControllers]; //viewControllers is an array of UINavigationControllers
I tried setting the property tintColor in myAppdelegate like this
[[myNavigation navigationBar] setTintColor:[UIColor redColor]];
But this didn't work as expected and so I tried the same in my ViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
self.navigationItem.title = #"Test";
}
And still nothing happens. Please checkout the image below to see how navigation bar looks now.
Appreciate your help.
Cheers
Jugs
As a work around you may can use image on place of tint color please check below code for same.
if ([myNavigation respondsToSelector:#selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) {
[myNavigation setBackgroundImage:[UIImage imageNamed:BAR_IMAGE_NAME] forbarMetrics:UIBarMetricsDefault];
}else {
[myNavigation insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:BAR_IMAGE_NAME]] autorelease] atIndex:0];
}
I'm trying to present a standard ViewController modally, but can't figure out how to do it. The view controller will have buttons which will ultimately trigger the dismissing actions, so I don't need to wrap it in a NavigationController. Also, I'm doing all of this programmatically, without .xibs.
Here's the code I'm using:
- (void)viewDidAppear:(BOOL)animated {
NSLog(#"view did appear within rootviewcontroller");
WelcomeViewController *welcome = [[WelcomeViewController alloc] init];
[self presentModalViewController:welcome animated:true];
[welcome release];
}
The problem is that I haven't set the WelcomeViewController's view, so loadView is not getting run, which means no content is being drawn to the screen.
Every example I find, including Apple's, uses either a .xib to initialize the ViewController, a NavigationController that adds a RootViewController, or both. My understanding is that loadView is called automatically for you in both of these scenarios.
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW3
Where do I configure my WelcomeViewController's view? Right there after the alloc/init? Inside WelcomeViewController's init method?
Thanks!
Where do I configure my WelcomeViewController's view?
Override the loadView method in your subclass. See View Controller Programming Guide for iOS.
Here's a simple example of how you can go about it without using NIBs:
In your AppDelegate didFinishLaunchingWithOptions:, you create an instance of your custom view controller and add it as a subview of window (pretty standard).
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
RootViewController *vc = [[RootViewController alloc] initWithNibName:nil bundle:nil];
[self.window addSubview:vc.view];
[self.window makeKeyAndVisible];
return YES;
}
When creating the vc instance, you use the designated initialiser which will be called on the new instance of the view controller. You don't specify any nibs because you will do your custom initialisation inside the method:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self.view setBackgroundColor:[UIColor orangeColor]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[label setBackgroundColor:[UIColor clearColor]];
[label setNumberOfLines:2];
[label setText:#"This is the vc view with an\norange background and a label"];
[label setTextColor:[UIColor whiteColor]];
[label setTextAlignment:UITextAlignmentCenter];
[self.view addSubview:label];
[label release];
}
return self;
}