最近在用iOS做自己的一个小项目,想实现更改主题的需求,没想到遇到navigationBar不透明的情况,而自己想弄成全透明,而且navigationBar上面的按钮能正常显示并且响应,反正自己捣鼓了半天也没弄好,无意中看到一个demo的例子,demo我忘了出自哪里了,反正正好实现了我的需求,所以记录下来,如果你也遇到和我一样的问题不妨试一试以下的代码,记得别做伸手党哦,成功了记得评论一下。
- (UIImage *)imageWithColor:(UIColor *)color
{
// 描述矩形
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
// 开启位图上下文
UIGraphicsBeginImageContext(rect.size);
// 获取位图上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 使用color演示填充上下文
CGContextSetFillColorWithColor(context, [color CGColor]);
// 渲染上下文
CGContextFillRect(context, rect);
// 从上下文中获取图片
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
// 结束上下文
UIGraphicsEndImageContext();
return theImage;
}使用方法:合适的地方调用
[self.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor clearColor]colorWithAlphaComponent:0]] forBarMetrics:UIBarMetricsDefault];
本文介绍了一种在iOS应用中实现导航栏全透明的方法,包括背景图片设置为完全透明的同时确保导航栏上的按钮能够正常显示及响应。通过提供的代码示例,开发者可以轻松地在自己的项目中复现这一效果。
1124

被折叠的 条评论
为什么被折叠?



