Missing drop shadow under UINavigationBar in combination with UITableView - uitableview

When I try to add a custom background to a navigation bar, their is no drop shadow on top of the table view. In the subviews everything is fine.
Here is my code I use.
// Create resizable images
UIImage *gradientImage44 = [[UIImage imageNamed:#"navbar"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// Set the background image for *all* UINavigationBars
[[UINavigationBar appearance] setBackgroundImage:gradientImage44
forBarMetrics:UIBarMetricsDefault];
// Customize NavBar Shadow
[[UINavigationBar appearance] setShadowImage:nil];
Not existing drop shadow
Existing drop shadow
Update:
Found this answer, but does not work. I have done it exactly in the same way :-(.

I fixed it, by reimplementing the the navigation controller. The view hierarchy looked like this.
UIView
+- Navigation View
+- Table View
I changed my UIView to an table view and added this to the UINavigation Controller. That fixed the missing shadow issue.

replace
[[UINavigationBar appearance] setShadowImage:nil];
with
[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:#"SomeShadowImage"]];
And it's for iOS 6.0 and later.

I followed #madcat's answer. In my case the UITableView or UITableViewController (embedded in a Container View) had to be above the Navigation Bar (dragged onto view) in the hierarchy. Like that the Navigation Bar was drawn with the shadow after/over the UITableView/Container

Related

Can't set my navigation bar background image

I want to change my table view navigation bar background image to some png, i created the nav bar png with the exact sizes of a default nav bar (I checked in interface builder).
This is my code (in my table view controller class, viewDidLoad method):
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:#"nav.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
Shouldn't this work..?
this is my table view settings in the interface builder if this help:
the top one is what i suppose to get and the bottom one is what im getting:
thanks!
UIImageView *backImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"nav.png"]];
[self.navigationController.navigationBar addSubview:backImgView];
Try to add Image in custom view
Why not use the existing nav bar? Don t think you need to create a new one... This code is working for me :
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"nav.png"] forBarMetrics:UIBarMetricsDefault];
Your TableViewController must be "embadded" in a navigation controller a.k.a connected to other ViewControllers as push segue... Then you get your nav bar automatically instead of creating one... I mean you create a nav bar and never add it to the view isnt it?

How to make a custom UIView to combine with UINavigationBar make it looks as one

As you can see below, the three buttons: STORE,AREA and All. These buttons looks like the UINavigationBar and them are combined together. I tried in storyboard, I added a custom UIView and try to set it's colour match the UINavigationBar background color. But it always has differences, and I can see the border line between UINavigationBar and the custom UIView. How to make a UIView to combine with the UINavigationBar to make it all looks like they are combined.
iOS 7 has shadow next to the navigation bar. Try below to remove that shadow:
if (IS_IOS7) {
[[UINavigationBar appearance]setShadowImage:[[UIImage alloc] init]];
}
You can change the navigation and status bar background only in iOS7. For this, you can use
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];
The size of the nav_bg.png should be 320*64 px
If you want to simply change the color of navigation bar you can use
[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];

UINavigationBar appearance setBackgroundImage Hides Status Bar

UIImage *gradientImage46 = [[UIImage imageNamed:#"navbar.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[UINavigationBar appearance] setBackgroundImage:gradientImage46
forBarMetrics:UIBarMetricsDefault];
I am using this to customize the appearance of my navigation bars in my app. However, this is causing the status bar to be solid black, showing nothing...time, carrier, battery, etc. Is this a bug, or did I implement this incorrect?
I have this running in a tab bar using MainWindow.xib method for interface. The first tab is just a navigation controller with a view controller inside it. Another tab is a Navigation Controller with a TableView Controller inside. If I go from one tab to the table view, and then back, the status bar appears.
The navbar.png is 320 x 44 pixels.
I also have this problem bringing my app from iOS 6 to iOS7, I solved by changing the code in this way:
instead
[UINavigationBar appearance] setBackgroundImage:gradientImage46
forBarMetrics:UIBarMetricsDefault];
i use
-(void) viewWillAppear:(BOOL)animated {
[self.navigationController.navigationBar setBackgroundImage:gradientImage46 forBarMetrics:UIBarMetricsDefault];
}

Issued changing the navigationbar image?

Currently, I am able to change the tint of the my UINavigationBar by
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:74/255.0f green:74/255.0f blue:74/255.0f alpha:1.0f];
However when I try to implement the following code to change the image of the UINavigationBar all together, I see no results..
UIImage *navImageBackground = [[UIImage imageNamed:#"texturedNav"] //This being my .png image
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:navImageBackground
forBarMetrics:UIBarMetricsDefault];
What am I doing wrong here?
You're probably calling the appearance delegate after the navigation bar is already on screen so your request is being 'ignored'. Or, at least, not causing a UI update.
Set the appearance before the navigation bar is shown. Or, force a UI refresh by hiding and showing the navigation bar (without animation).

How to ovverride UINavigationBar appearance if already customized

In the application delegate within my application I call the following method:
- (void)customizeAppearance
{
UIImage *gradientPortrait = [[UIImage imageNamed:#"gradient_portrait"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *gradientLandscape = [[UIImage imageNamed:#"gradient_landscape"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:gradientPortrait
forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:gradientLandscape
forBarMetrics:UIBarMetricsLandscapePhone];
}
This code allow to customize all the navigation bars within the application. Each bar becomes green since the image I use is green.
Now my goal is to ovveride the above configuration for a specific navigation bar.
In particular, during application lifecycle, I open a modal controller using UIModalPresentationFormSheet presentation style. This controller is presented within a UINavigationController. Since I need also to display the navigation bar attached with that UINavigationController, I would like to know how it is possible to customize that bar, without changing the global configuration I set in the application delegate.
I've tried to set both the tintColor property of the navigation bar (presented modally) to [UIColor blackColor] and the barStyle to UIBarStyleBlack, but they don't work. Only barbutton items are affected.
Thank you in advance.
P.S. I'm using iOS 5
First of all if you don't mind your image stretched you don't need to use the resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) method.
UIImage *gradientPortrait = [UIImage imageNamed:#"gradient_portrait"];
UIImage *gradientLandscape = [UIImage imageNamed:#"gradient_landscape"];
UIImage *someOtherImage = [UIImage imageNamed:#"someOtherImageName"];
Then, to achieve what you want:
Make sure you subclass the ViewController on with the custom Navigationbar will appear
Use the following to add the default image to all of your Navigation Bars
[[UINavigationBar appearance] setBackgroundImage:gradientPortrait forBarMetrics:UIBarMetricsDefault];
Use the following to add the specific image to navigationbars wich will appear above the subclassed ViewControllers
[[UINavigationBar appearanceWhenContainedIn:[YOURSUBCLASS class], nil] setBackgroundImage:someOtherImage forBarMetrics:UIBarMetricsDefault];
(you can call these methods from somewhere in your app delegate)
btw, if you just want to change the tint color you could just use the tintColor Property instead of all the images.
for more info check out the appearance sections of: UINavigationBar Class Reference
Subclass UINavigationBar with your custom class and se different appearance on it. Then use your custom class on that particular place where you what to have different look.

Resources