2013-10-03 37 views
1

我想設置navigaion欄的ShadowImage在我的應用程序委託:設置的ShadowImage在半透明導航欄

[[UINavigationBar appearance]setShadowImage:[UIImage imageNamed:@"navigation-bar-shadow"]]; 

這本身並不能正常工作。它只有當我設置navigaion欄的背景圖像以及工作原理:

[[UINavigationBar appearance]setBackgroundImage:[UIImage new]forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault]; 

這裏是我的問題...導航欄是transluncent,我想保持這種方式(無背景圖片)。但似乎我不能設置背景圖像而不設置shadowImage。

如何在半透明導航欄上設置shadowImage?

謝謝!

回答

0

是的,你不能設置的導航欄的的ShadowImage沒有設置其背景圖片:

見UINavigationBar的Documentation

對於自定義陰影圖像顯示,自定義背景圖片必須 也使用setBackgroundImage:forBarMetrics:方法進行設置。

但是你可以渲染的ShadowImage空圖像隱形:

[[UINavigationBar appearance] setShadowImage:[[UIImage alloc ]init]]; 
+0

但是,如果沒有自定義的背景圖像,這會失去半透明性,這仍然不起作用。有沒有辦法做到這一點,並保持良好的模糊效果? –

+1

我不明白這是如何回答這個問題 – Petar

0

響應標記,如果你想保持在導航欄半透明效果的答案不起作用。

一種解決方法我所做的是:

1)創建與我想要的顏色的圖片:

CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); 
UIGraphicsBeginImageContext(rect.size); 
CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextSetFillColorWithColor(context, [[UIColor blueColor] CGColor]); 
CGContextFillRect(context, rect); 

UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

然後我分配以此爲backgroundImageUINavigationBar

[[UINavigationBar appearance]setShadowImage:[[UIImage alloc] init]]; 
[[UINavigationBar appearance]setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 

注這使得導航欄完全透明。然後我做了以下模仿半透明的「模糊」的效果:

UIView *obfuscateView = [[UIView alloc] initWithFrame:CGRectMake(0,0,64,self.view.frame.width)]; 
[obfuscateView setBackgroundColor:[UIColor blueColor]]; 
[obfuscateView setAlpha:0.9f]; 
[self.view addSubView:obfuscateView]; 

這裏最主要的是你現在需要添加的導航欄後面附加視點,將模仿的模糊效果。這可以通過使用alpha透明度而不是實際上使用模糊視圖來進行計算而實現最小的性能。另請注意,此UIView需要具有相同的顏色。

這可以讓你做什麼,也可以使用具有不同alpha的漸變來創建淡入淡出效果。