在我的應用程序委託的didFinishLaunchingWithOptions函數中,我嘗試自定義導航欄的外觀。如何爲UINavigationBar設置不透明的1px高陰影圖像?
[UINavigationBar appearance].translucent = NO;
[[UINavigationBar appearance]
setBackgroundImage:[UIImage
imageWithColor:[UIColor whiteColor]
size:CGSizeMake(1.0f, 1.0f)
]
forBarMetrics:UIBarMetricsDefault
];
[UINavigationBar appearance].shadowImage = [UIImage
imageWithColor:[UIColor redColor]
size:CGSizeMake(0.5f, 0.5f)
];
我期待1px高的不透明紅色陰影。相反,我得到了2px高的半透明紅色陰影。如何使它看起來完全如我所願?我已經完成了UITabBar的類似外觀設置。另一方面,它表現得很好。
創建動態影像類別函數定義爲這樣:
+ (UIImage*)imageWithColor:(UIColor *)color size:(CGSize)size
{
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
你看着只是增加一個陰影的CALayer的?請參閱http://stackoverflow.com/questions/805872/how-do-i-draw-a-shadow-under-a-uiview – cmyr 2014-12-02 20:23:12
@cmyr'[UINavigationBar appearance] .layer.borderWidth = 0.5f; [UINavigationBar外觀] .layer.borderColor = [UIColor redColor] .CGColor;'在導航欄下不顯示任何內容。 – Pwner 2014-12-02 21:55:34
您是否將圖層的clipsToBounds設置爲NO? – cmyr 2014-12-02 23:21:47