关于swift的问题

1、为什么在appdelegate中手动设置windows 不生效

iOS13之后需要在sceneDelegate中设置

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
      
        let windowScene = scene as? UIWindowScene
        self.window = UIWindow.init(windowScene: windowScene!)
        self.window?.frame = UIScreen.main.bounds
        let loginVC = ViewController()
        
        let nav = UINavigationController.init(rootViewController: loginVC)
        self.window?.rootViewController = nav
        self.window?.makeKeyAndVisible()
        
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let _ = (scene as? UIWindowScene) else { return }
    }

2、为什么设置了navigationBar的颜色,确实渐变的模糊的?
如果直接这么设置

        self.navigationController?.navigationBar.backgroundColor = .red

你会发现你的navigationBar 是这样的


截屏2021-07-01 下午8.33.05.png

为什么呢?


截屏2021-07-01 下午8.34.16.png

你会发现又有个visualEffectView视图在前边遮挡?
那怎么才能不让它显示呢
通过设置navigationBar.setBackgroundImage

       self.navigationController?.navigationBar.setBackgroundImage(image, for: .default)

例:

 
        //修改导航栏背景图片(使用代码动态生成的纯色图片)
               let image = createImageWithColor(.red,
                                   frame: CGRect(x: 0, y: 0, width: 1, height: 1))
               self.navigationController?.navigationBar.setBackgroundImage(image, for: .default)


 //生成一个指定颜色的图片
     func createImageWithColor(_ color: UIColor, frame: CGRect) -> UIImage? {
         // 开始绘图
         UIGraphicsBeginImageContext(frame.size)
          
         // 获取绘图上下文
         let context = UIGraphicsGetCurrentContext()
         // 设置填充颜色
         context?.setFillColor(color.cgColor)
         // 使用填充颜色填充区域
         context?.fill(frame)
          
         // 获取绘制的图像
         let image = UIGraphicsGetImageFromCurrentImageContext()
          
         // 结束绘图
         UIGraphicsEndImageContext()
         return image
     }

你可能感兴趣的:(关于swift的问题)