I'm trying to change the background image of the Navigation Bar buttons for all the app. I guess it has to be done in the appDelegate.
Many thanks
You can use the appearance proxy for that. Just add this in your appDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//...
UIImage *myImage = [UIImage imageNamed:#"barButtonItemBackground.png"];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setBackgroundImage:myImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
return YES;
}
For more detail check the session 114 of the WWDC 2011
If your target is iOS 5, you can put this code in your AppDelegate:
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 5.0)
{
// iPhone 5.0 code here
UIImage *image = [UIImage imageNamed: #"image.png"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
Related
I am trying to set the icons of a UITabBarItem, but it is not working. By the way, I'm using Xcode 5 Beta for this project.
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
{
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
UIImage *tabBarBackground = [UIImage imageNamed:#"tabbar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"tabbar_selected.png"]];
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
}
return YES;
}
I am trying to get the icons to be white when both selected and unselected, but they are remaining gray in the unselected state.
Edit
I tried doing this, but now im getting the error "Expression Result Unused"
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
tabBarItem1.title = #"Home";
UIImage *graph = [UIImage imageNamed:#"graph.png"];
[tabBarItem1 initWithTitle:(NSString *)#"HELLO" image:(UIImage *)graph selectedImage:(UIImage *)graph];
tintColor sets the tint color of the background, you can't see this if you use a custom background image. If you don't want to have the system default gradient you have to use setFinishedSelectedImage:withFinishedUnselectedImage: and set your white image for both of them.
Like this:
UIImage *selectedImage = ...
UIImage *unselectedImage = selectedImage;
[tabBarItem setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
If you are setting those images in a Storyboard you can iterate over the existing UITabBarItems and change their images.
Put something like this into application:didFinishLaunchingWithOptions:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
NSAssert([tabBarController isKindOfClass:[UITabBarController class]], #"self.window.rootViewController must be a tabBarController");
for (UIViewController *viewController in tabBarController.viewControllers) {
UITabBarItem *tabBarItem = viewController.tabBarItem;
UIImage *tabImage = tabBarItem.image;
[tabBarItem setFinishedSelectedImage:tabImage withFinishedUnselectedImage:tabImage];
}
i need a little help with iphone rotation in landscape mode the navigation bar is not being stretched
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UIImage *navBarImage = [UIImage imageNamed:#"nav-bar.png"];
[[UINavigationBar appearance] setBackgroundImage:navBarImage
forBarMetrics:UIBarMetricsDefault];
UIImage *barButton = [[UIImage imageNamed:#"bar-button.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
[[UIBarButtonItem appearance] setBackgroundImage:barButton forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
UIImage *backButton = [[UIImage imageNamed:#"back-button.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,15,0,6)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButton forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
return YES;
}
You don't write this code in this method.First of all you have to return Yes in the method -
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
And then you have to use :
-(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation
{ if(orientation == UIInterfaceOrientationPortrait)
// here your code
}
My app has several viewControllers and for some of them, I would like to use a different navigationbar background image when they are pushed onto the navigation stack.
For example, when my mainViewController loads up, I have this:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(#"Setting toolbar for iOS 5+");
UIImage * navbarImage = [UIImage imageNamed:#"navbar01.png"];
[[UINavigationBar appearance] setBackgroundImage:navbarImage forBarMetrics:UIBarMetricsDefault];
}
Likewise, when my other viewController is pushed onto the navigation stack, I am using the same code as above, except using a different UIImage (navbar02.png).
However, the navigation bar doesn't change from the first image that I set. Is there any way to change the UINavigationBar background image when a new view appears?
Thank you!
Here is the link for similar question: UINavigationBar setBackgroundImage: forBarMetrics: Not Working
The answer is to use this:
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
rather than the code you have used.
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
// NSLog(#"%f",version);
if (version >= 5.0) {
UIImage *backgroundImage = [UIImage imageNamed:#"image.png"];
[self.navigationController.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
}
else
{
// UIImage *backgroundImage = [UIImage imageNamed:#"image.png"];
NSString *barBgPath = [[NSBundle mainBundle] pathForResource:#"NavBar" ofType:#"png"];
[self.navigationController.navigationBar.layer setContents:(id)[UIImage imageWithContentsOfFile: barBgPath].CGImage];
}
its working successfully for me ... maybe it will help you.
I have a simple piece of code that places a background image on the tabBar.
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tabBG.png"]];
[self.tabBarController.tabBar insertSubview:imageView atIndex:0];
[imageView release];
This works fine in iOS 4 but when testing in iOS 5, it doesn't work.
I'm trying to do the following:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tabBG.png"]];
NSString *reqSysVer = #"4.3";
NSString *iOSVersion = [[UIDevice currentDevice] systemVersion];
if ([iOSVersion compare:reqSysVer options:NSNumericSearch] !=NSOrderedDescending) {
// code for iOS 4.3 or below
[self.tabBarController.tabBar insertSubView:imageView atIndex:0];
}
else {
// code for iOS 5
[self.tabBarController.tabBar insertSubView:imageView atIndex:1];
}
[imageView release];
Alas, this isn't working... Can anyone offer a solution?
iOS5 offers the UIAppearance Proxy.
Also, it's best practice to switch your code based on the capability (in this case it's respondsToSelector) instead of iOS version - that's a fragile assumption (who's to say it doesn't change in the future).
You can set it for just that instance or globally for all tab bars:
// not supported on iOS4
UITabBar *tabBar = [tabController tabBar];
if ([tabBar respondsToSelector:#selector(setBackgroundImage:)])
{
// set it just for this instance
[tabBar setBackgroundImage:[UIImage imageNamed:#"tabbar_brn.jpg"]];
// set for all
// [[UITabBar appearance] setBackgroundImage: ...
}
else
{
// ios 4 code here
}
//---- For providing background image to tabbar
UITabBar *tabBar = [tabBarController tabBar];
if ([tabBar respondsToSelector:#selector(setBackgroundImage:)])
{
// ios 5 code here
[tabBar setBackgroundImage:[UIImage imageNamed:#"PB_MD_footer_navBg_v2.png"]];
}
else
{
// ios 4 code here
CGRect frame = CGRectMake(0, 0, 480, 49);
UIView *tabbg_view = [[UIView alloc] initWithFrame:frame];
UIImage *tabbag_image = [UIImage imageNamed:#"PB_MD_footer_navBg_v2.png"];
UIColor *tabbg_color = [[UIColor alloc] initWithPatternImage:tabbag_image];
tabbg_view.backgroundColor = tabbg_color;
[tabBar insertSubview:tabbg_view atIndex:0];
}
After reviewing various articles, I found the answer for anyone that's having the same problem:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tabBG.png"]];
if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {
//iOS 5
[self.tabBarController.tabBar insertSubview:imageView atIndex:1];
}
else {
//iOS 4.whatever and below
[self.tabBarController.tabBar insertSubview:imageView atIndex:0];
}
[imageView release];
Works like a charm! Enjoy.
I realize this has been solved, I'm posting this for others who had the same issue as me. I wanted to add a background image to the selected tab in a tabbar. Here is the solution:
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:#"tabbar.png"]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"tabbar-item.png"]];
The second line here adds a background image to the selected tab in a tabbar.
There is something new in iOS 5
forBarMetrics:UIBarMetricsDefault
Here is the apple doc on that
[[UINavigationBar appearance] setBackgroundImage:toolBarIMG forBarMetrics:UIBarMetricsDefault];
I'd like an image to take up all of a navigation bar. This is the navigation that comes with a navigation based app. It appears on the RootViewController with the accompanying UITableView. I've seen some examples of how this might work.
Set navigation bar title:
UIImage *image = [UIImage imageNamed:#"TableviewCellLightBlue.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar.topItem setTitleView:imageView];
The problem there is it only covers the title rather than the entire navigation bar.
There is also this thread: http://discussions.apple.com/message.jspa?messageID=9254241#9254241. Towards the end, the solution looks to use a tab bar, which I'm not using. It is that complicated to set a navigation bar background? Is there some other simpler technique?
I'd like to have a background for the navigation and still be able to use title text.
In your case, this solution found in another answer would work well.
With the "CustomImage" category added to UINavigationBar,
you can then just call:
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:#"yourNavBarBackground.png"];
[navBar setBackgroundImage:image];
This code should go in the method
- (void)viewWillAppear:(BOOL)animated
of the view controller where you want to have the custom image.
And, in that case you should better call:
[navBar clearBackgroundImage]; // Clear any previously added background image
before setBackgroundImage (otherwise it will be added multiple times...)
its changed for ios6, to make it work in ios 6 use:
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:#"image.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
UIImage *image = [UIImage imageNamed:#"YourImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar addSubview:imageView];
There is actually a much easier way to add a background image to any UIView class or subclass. It requires no class categorization or extension (subclassing), and you can do this on an "as needed" basis. For example, to add a background image to a view controller's navigation bar, do the following:
self.navigationController.navigationBar.layer.contents = (id)[UIImage
imageNamed:#"background.png"].CGImage;
You'll need to remember to add the Quartz Core framework to your project and add #import <QuartzCore/QuartzCore.h> wherever you need to do this. This is a much cleaner, simpler way to alter the drawing layer of anything that inherits from UIView. Of course, if you want to accomplish a similar effect for all navigation bars or tab bars, then subclassing makes sense.
UIImage *logo = [UIImage imageNamed:#"my_logo"];
UIImageView *logoView = [[UIImageView alloc]initWithImage:logo];
logoView.frame = CGRectMake(0, 0, 320, 37);
UINavigationController *searchNavCtrl = [[UINavigationController alloc] initWithRootViewController:searchViewController];
searchNavCtrl.navigationBar.barStyle = UIBarStyleBlack;
//searchNavCtrl.navigationItem.titleView = logoView;
//[searchNavCtrl.navigationController.navigationBar.topItem setTitleView:logoView];
[searchNavCtrl.navigationBar addSubview:logoView];
[logoView release];
Just add this line .
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"NavBar.png"] forBarMetrics:UIBarMetricsDefault];
Bam! One line and done.
I used cmp's solution and added some logic to remove it as I only wanted a custom background image on home screen within on view appear.
HomeViewController.m
UIImage *image = [UIImage imageNamed:#"HomeTitleBG.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.tag = 10;
UIImageView *testImgView = (UIImageView *)[self.navigationController.navigationBar viewWithTag:10];
if ( testImgView != nil )
{
NSLog(#"%s yes there is a bg image so remove it then add it so it doesn't double it", __FUNCTION__);
[testImgView removeFromSuperview];
} else {
NSLog(#"%s no there isn't a bg image so add it ", __FUNCTION__);
}
[self.navigationController.navigationBar addSubview:imageView];
[imageView release];
I also tried to use the suggested clearBackgroundImage method but couldn't get it to work so I gave the image a tag and then removed it in the other viewcontrollers on view will appear.
OtherViewController.m
UIImageView *testImgView = (UIImageView *)[self.navigationController.navigationBar viewWithTag:10];
if ( testImgView != nil )
{
NSLog(#"%s yes there is a bg image so remove it", __FUNCTION__);
[testImgView removeFromSuperview];
}
`
just go the view controller and paste in super viewdidload
and replace your image in mainlogo and then set the navigation title in set your image logo
- (void)viewDidLoad
{
[super viewDidLoad];
//set your image frame
UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,70,45)] ;
//set your image logo replace to the main-logo
[image setImage:[UIImage imageNamed:#"main-logo"]];
[self.navigationController.navigationBar.topItem setTitleView:image];
}
Add code in appdelegate did finish with launching method
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigation_or.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
}
else
{
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
// Uncomment to assign a custom backgroung image
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigation_or.png"] forBarMetrics:UIBarMetricsDefault];
// Uncomment to change the back indicator image
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#""]];
// Uncomment to change the font style of the title
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 0);
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,shadow, NSShadowAttributeName,[UIFont fontWithName:#"HelveticaNeue-Bold" size:17], NSFontAttributeName, nil]];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
}