ios - 透明导航栏 swift iOS

标签 ios swift navbar alpha

  1. This该项目目前的样子
  2. 这是我使用的代码

    func styleNavBar() {
    let navigationBarAppearace = UINavigationBar.appearance()
    navigationBarAppearace.tintColor = UIColor(red:1.0, green:1.0, blue:1.0, alpha:1.0)
    navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor(red:1.00, green:1.00, blue:1.00, alpha:1.0)]
    navigationBarAppearace.translucent = true
    
    navigationBarAppearace.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.3)
    navigationBarAppearace.setBackgroundImage(UIImage(), forBarMetrics: .Default)}
    

我尝试删除以下代码,但它看起来像 this .

navigationBarAppearace.setBackgroundImage(UIImage(), forBarMetrics: .Default)

我的问题是,如何让导航栏填满顶部?并且外观仍然相同

最佳答案

首先,为 UIImage 创建一个扩展,它创建具有指定大小的纯色图像。 将此扩展保留在类标签处的任何 ViewController 中

extension UIImage {

    class func imageWithColor(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
    }
}

然后使用以下方法自定义您的导航栏

func styleNavBar() {
    let navigationBarAppearace = UINavigationBar.appearance()
    navigationBarAppearace.tintColor = UIColor(red:1.0, green:1.0, blue:1.0, alpha:1.0)
    navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor(red:1.00, green:1.00, blue:1.00, alpha:1.0)]
    navigationBarAppearace.isTranslucent = true

    let colorImage = UIImage.imageWithColor(color: UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.3), size: CGSize(width: UIScreen.main.bounds.width, height: 64))
    navigationBarAppearace.setBackgroundImage(colorImage, for: .default)
}

希望这能解决您的问题。

关于ios - 透明导航栏 swift iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38935607/

相关文章:

ios - MKAnnotationView 没有显示在 map 上

ios - 收到推送通知时捕获

html - 使用自定义 Bootstrap 导航栏,如何使移动版本看起来与桌面版本不同

html - 中心的导航菜单

javascript - 如何在 jQuery Mobile 中将导航栏用作选项卡

ios - iOS 的 BT 4.0 LE 消费者文件

ios - 视觉节点之间的视觉寻路

ios - NSURLSession 方法不调用

ios - 将 segue 上的数据传递给 View Controller (Swift)

swift - WKWebView 在通过 PerfectHTTPServer 提供服务时无法加载静态文件