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
Related
I am adding a button in searchbar on right side by this code
[searchtextfield setRightView:customButton];
[searchtextfield setRightViewMode:UITextFieldViewModeAlways];
It works upto iOS 6,but in iOS 7 i can not see it. Any idea how to accomplish it in iOS 7.
If you have white background then may be its the issue that you can't see search bar, I faced same problem but managed to solve this by adding this code by setting table view background.
UIView *backgroundView = [[UIView alloc] initWithFrame:self.tableView.bounds];
backgroundView.backgroundColor = [UIColor clearColor];
self.tableView.backgroundView = backgroundView;
Hope it'll help. Thanks.
My custom TrueType font file that I brought into Xcode is not displaying correctly in the UINavigationBar. Instead of displaying the custom font, it displays the System font (Helvetica Bold).
RootViewController.m
self.title = #"Library";
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIFont fontWithName:#"adellebasic_bold.ttf" size:20.0], UITextAttributeFont,nil]];
I have also made sure that I copied it into Xcode correctly and I declared it under UIAppFonts in the Info.plist file. Also note that the code works if I set it to a UIFont that is included in the iPhone SDK but not a custom font brought in.
Does anyone have the slightest idea for what I'm doing wrong here?
Try this:
NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
[titleBarAttributes setValue:[UIFont fontWithName:#"adellebasic_bold" size:25.0f] forKey:UITextAttributeFont];
[self.navigationController.navigationBar setTitleTextAttributes:titleBarAttributes];
[self.navigationController.navigationBar setTitleVerticalPositionAdjustment:4.0f forBarMetrics:UIBarMetricsDefault];
Hope this helps.
actually the font file name and the font name are two different things.
Check This: iOS: How can i set a non system font family, style, size of UILabel? .
Try this name "Adelle Basic Bold".
I want to increase the Text size of UITabBarItem in my application.It is not visible clearly with its default color and size.
I tried this code but give me error -->UITabBar for instant message does not declare method with selector 'setTitleTextAttributes'.
Does any know how to do it?
[yourTabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[NSValue valueWithUIOffset:UIOffsetMake(0,0)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Helvetica" size:18.0], UITextAttributeFont, nil]
forState:UIControlStateNormal];
I think the default size is fully conveninet for the user. Anyway you want, maybe you should make your own <Tabbar> with UIView, UIButtons and UITabbar-style images.
setTitleTextAttributes:forState: is only available in iOS 5.0 or later. Please refer to UIBarItem Class Reference (UITabBarItem is subclass of UIBarItem). For prior versions of iOS, I think you'd better create your own customized tab bar.
And you may also want to try other methods from the answers in Changing font size of tabbaritem.
For that you should create dynamic tab bar using UITabbar class.
.Using this You can allow own size text ,image,color.
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'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