In my app, I have custom navigation bars using images. I load them up in viewWillAppear like so:
-(void)viewWillAppear:(BOOL)animated {
UIImage *gradientImage46 = [[UIImage imageNamed:#"navbar2.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[self.navigationController.navigationBar setBackgroundImage:gradientImage46 forBarMetrics:UIBarMetricsDefault];
[super viewWillAppear:animated];
}
On iOS 7, this is fine, but I just noticed on 6.1, the bar is much too large and looks like this:
Thoughts as to what's happening?
Related
I had tried following code to set background image of UINavigationBar.
It was working fine before Xcode 9 but in Xcode 9 image are not set properly.
UIImage *image = [UIImage imageNamed:#"headr_bg"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
Xcode 9 image 1
Xcode 9 image 2
[before Xcode 9]
may be its not perfect solution &
but i had old project in that i had fixed it by below code. Just updated navigation bar position and navigation view frame for ios version > 11.
CGRect navbarFrame = self.navigationController.navigationBar.frame;
CGRect navFrame = self.navigationController.view.frame;
navbarFrame.size.height = 44;
navFrame.origin.y = 20;
self.navigationController.navigationBar.frame = navbarFrame;
self.navigationController.view.frame = navFrame;
I had the same issue however I could not find a fix working with insets or with a layout based approach. Ultimately a quick work around was to create a secondary background image and pad the top of the original background with 20px with the previous color of your status bar (black in your case).
Then I simply added code to change the image depending if this is iOS 11+ like so:
NSString* navBarImg = #"navbar.png";
if (#available(iOS 11.0, *)) {
navBarImg = #"navbar_ios11.png";
}
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:navBarImg] forBarMetrics:UIBarMetricsDefault];
This may not work or may need tweaks depending if the status bar visibility is shown/hidden at some points. Hopefully this will help until a better approach is discovered.
I am develop in objective-C. I want to change the tab bar background like the following picture:
And the code is like the following:
UIImage *tabBarBackground = [UIImage imageNamed:#"tabbaritem_background.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
But after setting the background image , the background is not at the correct place like the following:
The background image should be place at the bottom like the background in above picture.
Did I missing something ? Can someone help me ?
Thanks in advance.
I think is somewhere you go wrong, check if is this steps:
In the storyboard change the ViewController's background color for test.
Embed the ViewController in Tab Bar Controller
In the ViewController.m you can set the tabbar bacground color:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[UITabBar appearance] setBackgroundColor:[UIColor grayColor]]; // Here you can set the converted color form image, make sure the imageSize fit.
}
The result is below:
I think the method - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode you can try, because your backgroudimage's size is not equal to tabbar'size.
Try this method to change your image to a ScaleImage.
+(UIImage *)getScaleImageNamed:(NSString *)name{
UIImage *nomalImage = [UIImage imageNamed:name];
CGFloat hInset = floorf(nomalImage.size.width / 2);
CGFloat vInset = floorf(nomalImage.size.height / 2);
UIImage *res = [nomalImage resizableImageWithCapInsets:UIEdgeInsetsMake(vInset, hInset, vInset, hInset)];
return res;
}
Steps you may miss, hope that can help.
Make sure you have imported the background image (e.g. in Assets.xcassets)
Use resizableImageWithCapInsets: to resize the background image
Put the UIAppearance settings in AppDelegate.m:
[[UITabBar appearance] setBackgroundImage:[[UIImage imageNamed:#"tabbaritem_background.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]];
is there a way to use a different images for the ipad landscape/portrait mode? My navigationbar contains a logo in the middle and while it works great on the iphone with different images, I can't use different images for the iPad so the logo isn't centered when you turn the device.
Alternatively, I can use a plain background image and maybe replace the navigation bar title with an image or center a button but I also couldn't manage to do that either. (I don't have a UINavigationController subclass).
here is my code that is doing the trick so far inside the app delegate:
if ([UINavigationBar respondsToSelector:#selector(appearance)]) {
// Create resizable images for iphone
UIImage *navbarImage44 = [[UIImage imageNamed:#"nav_bar"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *navbarImage32 = [[UIImage imageNamed:#"nav_bar_landscape"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// Overide for iPad
// In theory navbar_bg_landscape~iPad should work
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//use iPad specific image extending last pixel for landscape mode
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:#"nav_bar_ipad" ]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]
forBarMetrics:UIBarMetricsDefault];
}else {
// Set the background image for *all* UINavigationBars
[[UINavigationBar appearance] setBackgroundImage:navbarImage44
forBarMetrics:UIBarMetricsDefault];
// Never called on iPad, used for landscape on iPhone
[[UINavigationBar appearance] setBackgroundImage:navbarImage32
forBarMetrics:UIBarMetricsLandscapePhone];
}
}
I didn't get any response on this so I found a work around which is to resize the image on landscape. I'm ok with that for now. I'm resizing my portrait background image - here is my code for iPad
// Create resizable images
UIImage *ipadTabBarBG = [[UIImage imageNamed:#"nav_bar_ipad.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, -260, 0, 0)];
// Set the background image for *all* UINavigationBars
[[UINavigationBar appearance] setBackgroundImage:ipadTabBarBG
forBarMetrics:UIBarMetricsDefault];
I'm using a popover controller in my iPad app. In iOS 5, the popover looks like this:
In iOS 4, it looks like this:
How can I get rid of this weird black background?
Update: I'm pretty sure this is a self-inflicted wound, as other apps my company produces do not have this weird black nav bar problem, even when running in iOS 4. We must have code somewhere that changes this background, but I'm not sure how that would even be done.
It was indeed a self-inflicted wound. If anybody's interested, this category:
#implementation UINavigationBar (BackgroundImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"top-toolbar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
... will let you customize the navigation bar in pre-iOS 5, while simultaneously driving you crazy if you forget that you put it there yourself.
i know that this question looks the same as other around here, but none of them say anything about incompatibility between ios4 and ios5.
In my app i want to customize the pattern of the navigation Bar, but my deployment target is ios4, so i am using the code bellow above the implementation of the appDelegate.m to do this:
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor blackColor];
UIImage *image = [UIImage imageNamed: #"nav_bar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;
}
#end
When i run the app using the 4.3 simulator, it works properly, but when i simulate in ios5, it doesnt work, the nav bar goes back to the default color.. any help?
Thanks.
Here's a category that will work on both iOS 4 and 5:
#implementation UINavigationBar (CustomBackground)
- (UIImage *)barBackground
{
return [UIImage imageNamed:#"top-navigation-bar.png"];
}
- (void)didMoveToSuperview
{
//iOS5 only
if ([self respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)])
{
[self setBackgroundImage:[self barBackground] forBarMetrics:UIBarMetricsDefault];
}
}
//this doesn't work on iOS5 but is needed for iOS4 and earlier
- (void)drawRect:(CGRect)rect
{
//draw image
[[self barBackground] drawInRect:rect];
}
#end
I'd recommend using your existing code when running on iOS4, and using the new iOS5 features to customize the bar under iOS5.
See here: UINavigationBar's drawRect is not called in iOS 5.0
For iOS5 you can use this approach:
if([navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
[navigationBar setBackgroundImage:[UIImage imageNamed: #"nav_bar.png"]; forBarMetrics: UIBarMetricsDefault];
}
I usually put it into appDelegate.