导航项SWIFT的问题

时间:2018-05-28 07:47:55

标签: swift4

因为我需要让导航栏透明,所以一切正常但现在NavBar项目正在逐渐消失。

如何管理?

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.navigationItem.title = "Property Details"
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.bold)]
    self.navigationController?.navigationBar.tintColor = UIColor.black
    self.navigationController?.navigationBar.alpha = 0.5

}

enter image description here

1 个答案:

答案 0 :(得分:1)

您必须在初始视图控制器上添加故事板中的导航控制器,或者每当您出示控制器时都必须添加导航控制器。

希望这对你有用

删除alpha = 0.5,对于导航栏透明度,您需要执行以下操作:

// This is color extension to get image from color
public extension UIColor {

    func convertImage() -> UIImage {
        let rect: CGRect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(rect.size)
        let context: CGContext = UIGraphicsGetCurrentContext()!

        context.setFillColor(self.cgColor)
        context.fill(rect)

        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }
}
///This is tranceparent image which is get from color
let image = UIColor.init(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.2).convertImage()
self.navigationController?.navigationBar.setBackgroundImage(image, for: .default)

你需要在你的代码中添加UIColor扩展,然后在控制器中添加底部的两行,你可以使用UIColor中的增加或减少alpha调整你的褪色背景视图,现在我已经设置了0.2 alpha