在我的应用程序委托的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