我想要一个纯色导航栏,所以我打电话给
let image = UIImage.imageWithColor(color: UIColor.orange)
navigationController.navigationBar.setBackgroundImage(image, for: .default)
imageWithColor
方法生成纯色图像。
但是imageView的alpha是0.909804。
我设置了另一张图片再试一次。
我知道translucent = false
可以解决这个问题,但它会改变视图的布局。
有没有办法设置不透明的navigationBar而不设置translucent = false?
谢谢!
答案 0 :(得分:1)
您可以更改navigationBar的背景颜色
navigationBar.backgroundColor = UIColor.white
答案 1 :(得分:0)
尝试更改此方法 imageWithColor
替换此行:
UIGraphicsBeginImageContext(rect.size); // check this line in fuction
。通过强>
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, 0);
答案 2 :(得分:0)
我做了一个功能来实现这一点,在默认/出口导航栏中我用这个图像设置了纯色,它对我来说很好。
func getImageWithColor(color: UIColor, size: CGSize) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: size.width, height:
size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
再试一次这个。像这样打电话。
let image = self.getImageWithColor(color: UIColor.orange, size:
UIScreen.main.bounds.size)
self.navigationController?.navigationBar.setBackgroundImage(image,
for: .default)
答案 3 :(得分:0)
在AppDelegate.swift
中,在didFinishLaunchingWithOptions
中添加此行:
UINavigationBar.appearance().isTranslucent = false
然后,在viewController viewDidLoad
中添加以下行:
extendedLayoutIncludesOpaqueBars = true
这应该会给你一个不透明的导航栏,其行为类似于半透明导航栏。