I'm using Xcode 4.4.1 and iOS 5.
I have set the Navigation bar for several viewcontrollers using the storyboard.Now I want to set a background image for all those navigationbars.
so in my AppDelegate.m I put this,
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
UIImage *naviImg=[UIImage imageNamed:#"first.png"];
[[UINavigationBar appearance]setBackgroundImage:naviImg forBarMetrics:UIBarMetricsDefault];
}
But it not changes any thing in any Viewcontroller's navigationbar.
Can any one tell me how to do that, I'm very new to iOS development.
Thanks
Firstly,set the IBOutlet for the navigation bar in the viewController.h where you want to change the image.
IBOutlet UINavigationBar *navigationBar;
Then in viewDidLoad write this code,
[self.navigationBar setBackgroundImage:[UIImage imageNamed:#"wall.jpg"] forBarMetrics:UIBarMetricsDefault];
Related
Is there a way to create a global UIView as background with the use of a StoryBoard?
I am updating my App, and making iOS 5 as the minimum so I can use ARC and also StoryBoards.
However, in my App I used a MainView.xib which I loaded as my rootviewcontroller, and any subsequent view was transparent so the background (and a button/copyright notice) were always visible.
I don't seem to be able to figure out how to do this. I can add the Subview to the rootview controller in the AppDelegate, but as soon as I seque to the next view it is gone.
Any suggestions how this can be done?
One way you can do this by adding an image view as a subview of the window itself, and making the backgrounds of all the view controllers clear.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIImageView *iv = [[UIImageView alloc] initWithFrame:self.window.frame];
iv.image = [UIImage imageNamed:#"BlueGreenGradient.tiff"];
[self.window addSubview:iv];
return YES;
}
I know I can change the tint color of navigation bar in xib file easily. But the problem is that if i have to change in a lot of places.
Hence i tried using the (fancy) category and overriding the draw rect method.
#import "UINavigationBar+customTint.h"
#implementation UINavigationBar (customTint)
-(void)drawRect:(CGRect)rect{
self.tintColor = [UIColor redColor];
}
#end
I have imported it in the corresponding view controller as well. What am i doing wrong?
Whats the right way of doing it?
THanks in advance
That is one way of doing it. It would probably just be easier in you ViewController where ever you need to change it add the line:
self.navigationController.navigationBar.tintColor = [UIColor redColor];
Using a category for just one line is probably overkill.
EDIT:
If you do want to use a category, you may need to call setNeedsDisplay on the navigation bar. or override another method and call it.
Something like ,
[self.navigationController.navigationBar setNeedsDisplay];
Also according to Apple's documentation
In iOS 5, the UINavigationBar, UIToolbar, and UITabBar implementations have changed so that the drawRect: method is not called unless it is implemented in a subclass. Apps that have re-implemented drawRect: in a category on any of these classes will find that the drawRect: method isn't called. UIKit does link-checking to keep the method from being called in apps linked before iOS 5 but does not support this design on iOS 5 or later.
Apps can either:
Use the customization API for bars in iOS 5 and later, which is the preferred way.
Subclass UINavigationBar (or the other bar classes) and override drawRect: in the subclass.
The best way to therefore go about this would be to place this in your ApplicationDidFinishLaunching
NSString *reqSysVer = #"5.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
[[UINavigationBar appearance] setTintColor:myColor];
And also leave your DrawRect, so that it will work on iOS less than 5
If you're using iOS5 then the correct way to do it is to use the UIAppearance proxy.
[[UINavigationBar appearance] setTintColor:myColor];
Details here: https://developer.apple.com/library/ios/#documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html
I am using following code to set the background image for the navigation bar for iPhone OS 5:
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"tab bar.jpg"] forBarMetrics:UIBarMetricsDefault];
If I create the build for iPhone running OS Version 5...will it pose any problem if someone download the app and try to run on the device running OS version lesser 5?
Thanks
The method setBackgroundImage:forBarMetrics: is not available on iOS 4 or lower.
Thus calling this method will crash your app.
Just check if the object respons to selector:
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)]) {
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"tab bar.jpg"] forBarMetrics:UIBarMetricsDefault];
}
Beware that the over ridding the drawRect: method used in iOS 4 and lower does not work in iOS 5. Thus you have to implement both ways to get your app working on iOS 4 and 5.
According to the documentation:
- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics
Available in **iOS 5.0 and later.**
So you should check for the current iOS version and on IOS 5 use:
setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics.
and if you want to set the background in 4.0 here is the answer describing how to do it wither by adding a subview or using a category:
iPhone - NavigationBar Custom Background
// Add the QuartzCore frameowrk in .h file
#import <QuartzCore/QuartzCore.h>
// If you want set different image for every view.Write this code in viewWillAppear method
// NavigationBar background image.
if ([self.navigationController.navigationBar respondsToSelector:#selector( setBackgroundImage:forBarMetrics:)])
{
//For iOS >= 5
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"topbar.png"] forBarMetrics:UIBarMetricsDefault];
}
else {
//For iOS < 5
NSString *barBgPath = [[NSBundle mainBundle] pathForResource:#"topbar" ofType:#"png"];
[self.navigationController.navigationBar.layer setContents:(id)[UIImage imageWithContentsOfFile: barBgPath].CGImage];
}
I'm currently building a universal app.
After the user taps a button, I present some settings in a modal View in the following way:
viewCtrl.modalPresentationStyle = UIModalPresentationFullScreen;
viewCtrl.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:viewCtrl animated:YES];
On the iPhone and iPad both Transitionsstyles look the same. Now the problem is, on the iPad the background, behing the tilting view, is white. This white background just looks ugly.
On the iPhone it is black, as it is supposed to be.
I also tried adding a black backgroundview to my [UIApplication window]
UIView *bgView = [[UIView alloc] init];
[bgView setBackgroundColor:[UIColor blackColor]];
[window addSubview:bgView];
but it doesn't make a difference.
I think it is as simple as setting the UIWindow background color to black. For example, if you open the MainWindow.xib in IB, you should be able to set the background there - or in your AppDelegate you should be able to set the background of the UIWindow.
I have implemented a custom UINavigationbar using this code (placed in the top of my AppDelegate.m file):
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"NavigationBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
And it works great! The problem is, when I push a particular class of view controller, I would like to remove the custom navigationbar and show the standard one (or alternatively change the NavigationBar.png).
But I can't figure out how to do this, and googling isn't really helping.
Has anyone done this?
Many thanks,
Brett
Rather than use the drawRect override, place this bit of code in each view controller inside the navigation controller. Then just change the image that is used in each view controller you want it to be different:
self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:#"NavigationBar.png"].CGImage;
You will have to include Quartz core, so include this line in the imports of your view controllers header file:
#import <QuartzCore/QuartzCore.h>