Switching to iOS5, I wanted to make use of a custom styled UINavigationBar using the following code snippet:
if ([navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:
forBarMetrics:)]) {
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"NavigationBar.png"]
forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"NavigationBarLandscape.png"]
forBarMetrics:UIBarMetricsLandscapePhone];
}
This works quite well, but: my custom image has rounded corners (c.f. the attached images) which causes the following:
Starting in portrait mode, it looks fine.
Changing to landscape mode also looks good.
Changing back to portrait mode however does not seem to change the image to NavigationBar.png, thus there is no rounded corner on the top right.
Any advice on how to solve this problem is appreciated.
// edit
Adding the following code to my viewController removes this "bug" but this cannot be the right solution!?!
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation
duration:duration];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"NavigationBar.png"]
forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"NavigationBarLandscape.png"]
forBarMetrics:UIBarMetricsLandscapePhone];
}
As iOS6 introduces rounded corners by default, this problem will disappear soon.
Related
When I try to change the background image for a second time for a tab view, nothing happens.
I'm using:
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:newBarImageBackGround];
It seems that you need to use something like the following for changing the tab bar's background. Unfortunately, this doesn't fly with appearance.
[[self.tabController tabBar] setBackgroundImage:[UIImage imageNamed:newBarImageBackGround]];
Your code should work for the first time though. Reference: Link
Note: [self.tabController tabBar] should be replaced with whatever reference to a tab bar you're using.
EDIT
You could also try this, but I haven't tested it so I don't know if it'll work.
[[UITabBar appearance] setBackgroundImage:nil];
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:newBarImageBackGround]];
Recently I've updated my Xcode IDE to the latest to have iOS 7.1 SDK. For some reason, after recompiling my app with the new Xcode I get this wired UINavigationBar background colour.
I'm using storyboard to push the secondary view controller from home screen. The secondary view controller is a static table of settings, also built using storyboard, and contains the following code in its viewDidLoad method:
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = nil;
I haven't done any other changes to this screen or any other screens in the app.
This is how it used to look like before:
This is how it is look like now:
Does anyone have any idea what could have caused this?
Cheers,
EDIT: #Leo Natan -
When I'm trying to set the color myself, for example, by replacing this line:
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
with this line:
[self.navigationController.navigationBar setBackgroundColor:[UIColor redColor]];
I get this: the red color applied only on the header without the status bar, what can I do?
Latest Xcode comes with iOS7.1 SDK, in which Apple once again changed how the color is calculated. You will have to play again with your color to achieve a similar look in iOS7.1.
Maybe you should take another approach. Use the Appearance Proxy in the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions. Thats how I do it:
//Navigationbar
[[UINavigationBar appearance] setBarTintColor:lightOrangeColor];
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#"navbarAssets-backArrow-iOS7"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"navbarAssets-backArrow-iOS7"]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes: #{NSForegroundColorAttributeName : [UIColor whiteColor]}];
I want to create custom buttons for UIToolbar (and a custom background color for UIToolbar) in XCode.
So far, I've done it like this: create UIButton, then set an image to it and then drag it to UIToolbar. It becomes UIBarButtonItem after this, but still it has an image so it's fine for me.
The problem is, that for some reason buttons' images became darker than original images (I can see it because my toolbar background is exactly the same as buttons' background), but when I press the button, I see that its image becomes original-like and then after a sec turns back to the darker one.
I've tried to do as said in this question: Image in UIButton is darker tharn original, setting highlighted image in XCode, but after this it stopped 'changing to original' at all.
So, how can I solve this? I don't really want to bring some GUI stuff like this in code if there's a way to do this by XCode... Any help would be appreciated.
EDIT:
That's the screen shot of what's going on:
And that's how they should look like with toolbar background + buttom image combined:
Now I did it in code like this:
#implementation UIBarButtonItem (CustomView)
+ (UIBarButtonItem*) barItemWithImage:(UIImage *)image target:(id)target action:(SEL)action
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
[button setImage:image forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* item = [[self alloc] initWithCustomView:button];
return item;
}
#end
And the calling code:
UIBarButtonItem *item =[UIBarButtonItem barItemWithImage:[UIImage imageNamed:#"settings"] target:nil action:nil];
[self.bottomBar setItems:#[item]];
For toolbar background I use this code:
[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:#"toolbar_background"]
forToolbarPosition:UIToolbarPositionAny
barMetrics:UIBarMetricsDefault];
The problem remains the same...
EDIT 2:
Wow, it seems like actually the UIToolbar is lighter than original...
EDIT 3:
Well. Created new project and tested the XCode storyboard-way (with UIButton), there is works fine... That's a mystery for me, totally lost here. Need some serious clarification here about possible reasons of this: my current project is huge and I'm not able to post all the code here...
Well, since nobody answered, I'll just say that I created a new project and the problem gone.
But it happened once again, and I totally don't know why. I did a really lazy and bad thing and changed image brightness for that unlucky button in some image editor =D
I'm using the following code to set the navbar's background:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"tile.png"] forBarMetrics:UIBarMetricsDefault];
This results in a nice tiled background such as you see here:
However, when I open up a UIImagePickerController, the background is warped somehow and we end up with something like this:
Anyone have suggestions on how to fix it?
Stretching instead of tiling: to Stop That
Create a class of type UINavigationBar
Create your custom UINavigationBar subclass.
Comment initwithFrameMethod
Add method DrawRect:
(void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed:#"bluebackground.jpeg"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
4.in AppDelegate.m, AppliocationdidFinishLaunching
Write
[self.navigationbar setValue:[[customBar alloc]init] forKeyPath:#"navigationBar"];
Adding Image By this way will not stretch the image
Refer this Link:
http://www.iosdevnotes.com/2011/09/custom-uinavigationbars-techniques/
http://designm.ag/tutorials/designing-a-custom-iphone-app-navigation-bar/
Hope this will help you.
I'm doing something like this:
UIImage *barBackgroundImage = [UIImage imageNamed:#"myImage.png"];
[[UINavigationBar appearance] setBackgroundImage:
backgroundImage
forBarMetrics:UIBarMetricsDefault];
And the resulting UINavigationBar is much larger than the standard navigation bar. I'm aware that my image size might be the problem. Is there any way to take care of this programmatically?
This is how I set the UINavigationController's navigationBar background image.
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navbar.png"] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setTintColor:LIGHTBROWN];
navbar.png is 320x44 and respectively 640x88 for navbar#2x.png.
Follow this link to make your code compatible with iOS4, 5 and 6: http://robert-varga.com/?p=735
You just have to make in Photoshop or other software a rectangular with the size of 320x44 or 640x88 (for retina display) and import it to your project