Skip to content

Instantly share code, notes, and snippets.

@jamztang
Last active December 23, 2015 06:09
Show Gist options
  • Save jamztang/6591926 to your computer and use it in GitHub Desktop.
Save jamztang/6591926 to your computer and use it in GitHub Desktop.
JTDropShadowFixNavigationViewController - Using custom drop shadows in UINavigationBar (fix for iOS 6.1)

Using custom drop shadows in UINavigationBar (fix for iOS 6.1)

This is an update to the Adding Drop Shadow on UINavigationBar which was written prior to iOS 6.0

Since iOS 6.0 Apple introduced the -[UINavigationBar shadowImage] property. We should probably leverage that, which are also UIAppearance selectors.

// Configure your images
UIImage *background = [UIImage imageNamed:@"titlebar44"];
UIImage *shadow = [UIImage imageNamed:@"titlebar-bottom-highlight"];

// Using the appearance proxy
// Note: setBackgroundImage:forBarMetrics has been documented to be
// **compulsory** to enable the displaying of the custom shadow image.
[[UINavigationBar appearance] setBackgroundImage:background forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:shadow];

You probably went with similar code, but the custom shadow just didn't show up, right?

The problem occurs because of the implementation on UINavigationBar, the clipsToBounds property is needed to be set to NO, and this step has been missing from the documentation. (At least I didn't find it)

Most probably you'll need to set self.navigationController.navigationBar.clipsToBounds = NO in every UIViewController subclass you wrote, a more convenience way is to subclass UINavigationController and have it do the job for you. And I've created a snippet for you.

<script src="https://gist.github.com/6591926.js?file=JTDropShadowFixNavigationViewController.h"> </script> <script src="https://gist.github.com/6591926.js?file=JTDropShadowFixNavigationViewController.m"> </script>

So now you can initialise your view controllers in code like this:

UIViewController *controller; // configure your view controller
UINavigationController *navController = [[JTDropShadowFixNavigationViewController alloc] initWithRootViewController:controller];

Or if you use storyboard, simply change the subclass of your UINavigationController to JTDropShadowFixNavigationViewController.

In fact there's an even simpler method you'll notice in the snippet.

This is a hack that adds a category method to always set clipsToBounds to NO for UINavigationBar. Enable it defining this constant somewhere in your code (recommended in .pch):

#define ENABLE_JTDROPSHADOW_GLOBAL_FIX 1

Let me know your thoughts!

Jamz

/*
* This file is part of the http://ioscodesnippet.com
* (c) Jamz Tang <jamz@jamztang.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
#import <UIKit/UIKit.h>
@interface JTDropShadowFixNavigationViewController : UINavigationController
@end
/*
* This file is part of the http://ioscodesnippet.com
* (c) Jamz Tang <jamz@jamztang.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
#import "JTDropShadowFixNavigationViewController.h"
@implementation JTDropShadowFixNavigationViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationBar.clipsToBounds = NO;
}
@end
#if ENABLE_JTDROPSHADOW_GLOBAL_FIX
@interface UINavigationBar (JTDropShadowFix)
@end
@implementation UINavigationBar (JTDropShadowFix)
- (void)awakeFromNib {
self.clipsToBounds = NO;
}
@end
#endif
Copyright (c) 2013 Jamz Tang <jamz@jamztang.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment