IOS 13 FWPopupView弹出框不弹出 windowScene新特性 解决方案

昨天集成这个FWPopupView的时候发现弹出框不弹出来,因为项目里的弹出框是加载到自定义的FWPopupWindow上的,但是FWPopupWindow压根没初始化成功,(能弹出来才见鬼)

然后用低版本项目集成,一下子就成功了,所以猜想是windowScene的问题
专业术语咱也不会说,话不多说上代码

前期准备:

1.首先在你的xcode11 ios 13的环境下,新建一个xcode项目
image.png
xcode 11, ios 13 新建的项目结构是下图这样:
image.png
2.打开终端用pod引入FWPopupView 和 IQKeyboardManagerSwift
image.png
3. 在pod里找到FWPopupWindow文件 并需要改两处
image.png
   @objc public class var sharedInstance: FWPopupWindow {
       struct Static {
           static let kbManager: FWPopupWindow = { () -> FWPopupWindow in
               if #available(iOS 13.0, *) {
                          let windowScene = UIApplication.shared
                                          .connectedScenes
                                          .filter { $0.activationState == .foregroundActive }
                                          .first
                          if let windowScene = windowScene as? UIWindowScene {
                           return FWPopupWindow(windowScene: windowScene)
                   }
                         
               }
               return FWPopupWindow(frame: UIScreen.main.bounds)
               
           }()
           
       }
       return Static.kbManager
   }
image.png
 public override init(frame: CGRect) {
        super.init(frame: frame)
        self.setup()
        
    }
    
    @available(iOS 13.0, *)
    public override init(windowScene: UIWindowScene) {
        if #available(iOS 13.0, *) {
            super.init(windowScene: windowScene)
            setup()
        } else {
            // Fallback on earlier versions
            fatalError("")
        }
    }
    
    func setup() {
        let rootVC = FWPopupRootViewController()
        rootVC.view.backgroundColor = UIColor.clear
        self.rootViewController = rootVC
        
        self.windowLevel = UIWindow.Level.statusBar + 1
        
        let tapGest = UITapGestureRecognizer(target: self, action: #selector(tapGesClick(tap:)))
        //        tapGest.cancelsTouchesInView = false
        tapGest.delegate = self
        self.addGestureRecognizer(tapGest)
        
        let panGest = UIPanGestureRecognizer(target: self, action: #selector(panGesClick(pan:)))
        self.addGestureRecognizer(panGest)
    }
4、项目兼容ios13
image.png

项目会报一堆错


image.png

需要在函数前添加一行代码

@available (iOS 13.0, *)
image.png

这里也会报错 ,直接在类前添加 @available (iOS 13.0, *)


image.png

image.png
5.自定义根控制器
image.png
guard let windowScene = (scene as? UIWindowScene) else { return }
        self.window = UIWindow (windowScene: windowScene)
        self.window?.rootViewController = UINavigationController(rootViewController: ViewController())
        self.window?.backgroundColor = UIColor.yellow
        self.window?.makeKeyAndVisible()
6. 最后在根控制器里随便写一个例子调用一个弹框
image.png
//
//  ViewController.swift
//  PopViewTest
//
//  Created by uu on 2019/11/14.
//  Copyright © 2019 UU. All rights reserved.
//

import UIKit
import FWPopupView

class ViewController: UIViewController {
    
    lazy var alertImage: FWAlertView = {
        
        let block2: FWPopupItemClickedBlock = { [weak self] (popupView, index, title) in
            
            if index == 1 {
                // 这边演示了如何手动去调用隐藏
                self?.alertImage.hide()
            }
        }
        
        // 注意:此时“确定”按钮是不让按钮自己隐藏的
        let items = [FWPopupItem(title: "取消", itemType: .normal, isCancel: true, canAutoHide: true, itemClickedBlock: block2),
                     FWPopupItem(title: "确定", itemType: .normal, isCancel: false, canAutoHide: false, itemClickedBlock: block2)]
        // 注意:添加自定义的视图,需要设置确定的Frame值
        let customImageView = UIImageView(image: UIImage(named: "audio_bgm_4"))
        customImageView.frame = CGRect (x: 0, y: 0, width: 100, height: 100)
        customImageView.backgroundColor = UIColor.blue
        let vProperty = FWAlertViewProperty()
        vProperty.touchWildToHide = "1"
        
        let alertImage = FWAlertView.alert(title: "标题", detail: "带自定义视图", inputPlaceholder: nil, keyboardType: .default, isSecureTextEntry: false, customView: customImageView, items: items, vProperty: vProperty)
        return alertImage
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem (title: "点我吖", style: .done, target: self, action: #selector(onClickPop))
    }
    @objc func onClickPop() {
        print(self.alertImage.frame)
        self.alertImage.show()
        print(self.alertImage.frame)
    }
}

成功在xcode 11, ios 13 上集成~~~~

注意!!!!!!!最主要的步骤是3

不懂的私信 溜了溜了

你可能感兴趣的:(IOS 13 FWPopupView弹出框不弹出 windowScene新特性 解决方案)