Setting background image of navigation bar for two different OSs - iphone

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];
}

Related

uiview appears distorted in ios7 but works fine for ios6 and others

UIView of my app appears fine in ios6 but when it comes to ios7 the entire view is distorted. In ios7 the whole view is lifted upwards.
EDIT :
Then I applied this code:
float SystemVersion=[[[UIDevice currentDevice] systemVersion] floatValue];
if(SystemVersion<7.0f)
{
//Currently your app is running in IOS6 or older version. So you need not to do anything.
}
else
{
// Currently your app is running in IOS7. Do the following.
CGRect TempRect;
for(UIView *sub in [[self view] subviews])
{
TempRect=[sub frame];
TempRect.origin.y+=20.0f; //Height of status bar
[sub setFrame:TempRect];
}
}
but still there is no change. How do I resolve this?
Try this way..
IOS & DISPLAY
OR
This..
Status Bar Issue
This way make the your All subviews as +20 pixels i.e.Increase the +20 of y-axis for all views
Set the deleta for the ios 7. so, it will display the good in both ios 6 & 7.
//Give it frame if navigation bar and status bar both displaying on screen
// first check if device have ios 7
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7.0"))
{
self.view.frame=CGRectMake(0,64,320,self.view.height);
}
try my answer...write that code in ViewdidLoad method..
Status Bar issue
Happy Coding!!!
Please see below note
Using xcode 5,
click on xib,Go to file inspector and change setting as below
Deployment target as ios 7
here,You need to crate two xib view based on view as ios 7 and later and view as ios 6 and earlier and white condition based on xib call
Define below code in .m file
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
if view as IOS 7.0 and later
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7.0"))
//Here call ios 7 xib
}else{
//Here call ios 6 and earlier xib
}
You have to create two xib for ios6 and ios7.
than You have to check condition if device is of ios7 than call ios7 xib otherwise go for ios6.

iOS 7 MPMoviePlayerController change name of Done button

In iOS 6 we change name of done button in MPMoviePlayerController next way:
id mpBarAppearance = [UIBarButtonItem appearanceWhenContainedIn:[MPMoviePlayerViewController class], nil];
[mpBarAppearance setTitle:#"ГОТОВО"];
[mpBarAppearance setTitlePositionAdjustment:UIOffsetMake(1.5f, 1.f) forBarMetrics:UIBarMetricsDefault];
[mpBarAppearance setTitlePositionAdjustment:UIOffsetMake(1.5f, 1.f) forBarMetrics:UIBarMetricsLandscapePhone];
Now in iOS 7 this crash app, under debbug it no raise exception and I cann't even understand why ?
It's because you can't use the appearance proxy to customise the title of UIBarButtonItems. Otherwise, all the bar buttons contained in the given parent would be named the same. You can customise other appearance parameters such as the background images, title position and so on.
The appearance proxy reference for UIBarButtonItem can be found here

how to create iphone application which is compatible for iphone 4 and 5 screen sizes [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to develop or migrate apps for iPhone 5 screen resolution?
I have two sets of images.One set of images to iphone 4 background images and other set of images to iphone 5.How can I use these two image sets to create my iphone application which wants to be compatible with both screen sizes ?
do I want to check the iphone version and apply images using some coding ?
Hope you can help.Thanks for the help.
I am using a class extension called UIDevice. You can find the source here: https://github.com/erica/uidevice-extension
I have set up a neat little method which layouts the view according to screen size.
Example:
- (void)setupGUI
{
UIImage *backgroundImage;
if ([[UIDevice currentDevice] platformType] == UIDevice5iPhone)
{
//4 inch screen
backgroundImage = [[UIImage alloc] initWithCGImage:[UIImage imageNamed:#"main_background-568h#2x"].CGImage scale:2.0 orientation:UIImageOrientationUp];
}
else
{
//3.5 inch screen
backgroundImage = [[UIImage imageNamed:#"main_background"] retain];
}
self.view.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
[backgroundImage release];
}
For the splash you can use the suffix -568h#2x
For UIImage you can use:
[UIImage imageNamed:#"tryImage"]
And you can have tryImage.png, tryImage#2x.png and tryImage-568h#2x.png, but it seems not to be working as we would like.
You can also use a trick like this: https://gist.github.com/3782351

How to set the background of navigationbar in ios5 (Retina)?

I use this code to set the background.
It do change the background, but there is some problem with Retina.
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navi_bar.png"] forBarMetrics:UIBarMetricsDefault];
I have two pngs: navi_bar.png (320*44) and navi_bar#2x.png (640*88)
The background of navigation bar always uses image navi_bar.png, even in Retina.
If I use this code:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navi_bar#2x.png"] forBarMetrics:UIBarMetricsDefault];
The result is this in Retina:
The background has the double height...
I have spent the whole morning on this problem.
Can anybody help me?
Thanks!
Try this
UINavigationBar *navBarName = [[self navigationController] navigationBar];
UIImage *backgroundImg = [UIImage imageNamed:#"navi_bar"];
[navBarName setBackgroundImage:backgroundImg forBarMetrics:UIBarMetricsDefault];
You don't have to write #"navi_bar#2x.png". iOS will automatically pick up the right image for retina display. Just use #"navi_bar.png".
EDIT- This SO question will help
I had the same issue: NavBarBg.png is applied for retina device even though NavBarBg#2x.png is available. But it seems to be related with simulator only. When I run the app on device NavBarBg#2x.png is applied correctly.
You can use: [[UINavigationBar appearance] setBackgroundImage:aImg forBarMetrics:UIBarMetricsDefault];
in iOS 5
This is weird you should check whether you have added the 2x image properly or not

What should I do to adapt my app to iOS 5.0 keeping compatibility with iOS 4

I've started playing with iOS 5 today and I've seen that XCode 4.2 only let me select iOS 5 as Base SDK but not iOS 4.
Until now I've overwriting drawRect: method in UINavigationBar to customize its appearance but iOS 5 doesn't call that method anymore. Now I've to use [UINavigationBar appearance] to do it (which I think is much better). However, appearance method is only available in iOS 5 so if I use it my app it crashes when executing on iOS 4. What should I do? Do I have to check iOS version with macros in every place I use a iOS 5 method?
Thank you,
Ariel
The answer to your first question is: You must use iOS5 (or Latest iOS SDK) as your base SDK, but you set your minimum supported iOS version under Deployment Target. There you can set iOS4.0 (or whatever you want).
The correct way to deal with your second question is to test for capability, not version. So, something like this would work in say, your application:didFinishLaunchingWithOptions: method:
// iOS5-only to customize the nav bar appearance
if ([[UINavigationBar class] respondsToSelector:#selector(appearance)]) {
UIImage *img = [UIImage imageNamed: #"NavBarBackground.png"];
[[UINavigationBar appearance] setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
}
You will then be compiling this against the iOS5 SDK, so the use of appearance at all will be fine. But when this compiled code runs on a version of iOS before 5, it will be fine.
As said before, you can keep your drawRect: code as-is.
Another way to customized your header is like this.
UIImage *image = [UIImage imageNamed:#"header.png"];
if([navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
//iOS 5 new UINavigationBar custom background
[navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
}
else{
UIImageView *imgView = [[[UIImageView alloc] initWithImage:image] autorelease];
[imgView setUserInteractionEnabled:NO];
[imgView setTag:TOOLBAR_TAG];
[navigationBar insertSubview:imgView atIndex:0];
}
Using the respondsToSelector you can know if the function is here.
You can put the same piece of code in both the drawRect: that iOS 4 uses and the proxy returned by [UINavigationbar appearance]. Two different code paths.
You can't do this with macros since both code paths have to be in place and the correct route to go depends on a run-time check.
Soooo... use something like this:
NSString *os_version = [[UIDevice currentDevice] systemVersion];
to get the iOS version you're currently running on and do the [UINavigationBar appearance] under 5 & newer, and you can fall back to the drawRect thing on iOS 4.
Also take advantage of downloading the 4.3 simulators for iPhone and iPad. Then you can crash faster when you accidentally use iOS 5 stuff on 4.3
--Tom