c_cpp JTDropShadowFixNavigationViewController - 在UINavigationBar中使用自定义阴影(针对iOS 6.1修复)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp JTDropShadowFixNavigationViewController - 在UINavigationBar中使用自定义阴影(针对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](http://ioscodesnippet.com/post/10437516225/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.
![](https://gist.github.com/jamztang/6591926/raw/0437477ed7efd34a81bcc66a47de30c95071d7cb/modify-class.png)
**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
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.
/*
* 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
/*
* 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
以上是关于c_cpp JTDropShadowFixNavigationViewController - 在UINavigationBar中使用自定义阴影(针对iOS 6.1修复)的主要内容,如果未能解决你的问题,请参考以下文章