【发布时间】:2014-12-02 20:08:48
【问题描述】:
在我的应用程序委托的 didFinishLaunchingWithOptions 函数中,我尝试自定义导航栏的外观。
[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)
];
我希望有一个 1 像素高的不透明红色阴影。相反,我得到了一个 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 添加阴影?见stackoverflow.com/questions/805872/…
-
@cmyr
[UINavigationBar appearance].layer.borderWidth = 0.5f; [UINavigationBar appearance].layer.borderColor = [UIColor redColor].CGColor;在导航栏下不显示任何内容。 -
您是否将图层的 clipsToBounds 设置为 NO?
标签: ios cocoa-touch uinavigationbar uiappearance