我难以在iPhone 5和5S之间整合UINavigationBar的barTintColor。我的两部手机都在iOS 7上。在下面的截图中,顶部是5S,底部是5. iPhone 5S显示极其半透明的效果,而iPhone 5显示更微妙的效果。在iPhone 5的导航栏后面只能看到非常暗的物体。
[[UINavigationBar appearanceWhenContainedIn:[UINavigationController class], nil]
setBarTintColor:[UIColor
colorWithRed:46.0 / 255.0
green:160.0 / 255.0
blue:152.0 / 255.0
alpha:0.8
]
];
我希望这两款手机看起来像iPhone 5.如果我要将barTintColor的alpha增加到1.0,iPhone 5的导航栏将变得完全不透明。这是预期的结果。虽然iPhone 5S的条形变得不那么半透明,但效果仍然太强烈。如何在不使其完全不透明的情况下进一步降低半透明度?
答案 0 :(得分:4)
正如评论中所讨论的,您会看到不同的行为,因为其中一个设备使用的是过时的iOS 7版本。 Apple在版本7.0.3中对条纹颜色处理方式进行了更改,现在考虑了alpha值。您应该专注于较新版本的iOS。
答案 1 :(得分:3)
如果您仍想在IOS 7.1中为导航栏设置alpha,我找到了解决方法。从为其设置了alpha的颜色创建图像,然后将此图像作为背景分配给导航栏:
1-这里是从颜色创建图像的方法:
-(UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
我在Creating a UIImage from a UIColor to use as a background image for UIButton
找到了它//create a colour and set its alpha:
UIColor *colorWithAlpha = [UIColor colorWithRed:(80/255.f) green:(146/255.f) blue:(84/255.f) alpha:0.2]; // light red colour
// create your background image:
UIImage *backgroundImage = [self imageWithColor: colorWithAlpha];
//set this image as a background image:
[self.navigationController.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init]; // to remove shadow