昨天集成这个FWPopupView的时候发现弹出框不弹出来,因为项目里的弹出框是加载到自定义的FWPopupWindow上的,但是FWPopupWindow压根没初始化成功,(能弹出来才见鬼)
然后用低版本项目集成,一下子就成功了,所以猜想是windowScene的问题
专业术语咱也不会说,话不多说上代码
前期准备:
1.首先在你的xcode11 ios 13的环境下,新建一个xcode项目

xcode 11, ios 13 新建的项目结构是下图这样:

2.打开终端用pod引入FWPopupView 和 IQKeyboardManagerSwift

3. 在pod里找到FWPopupWindow文件 并需要改两处

@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
}

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

项目会报一堆错

需要在函数前添加一行代码
@available (iOS 13.0, *)

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


5.自定义根控制器

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. 最后在根控制器里随便写一个例子调用一个弹框

//
// 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
不懂的私信 溜了溜了
网友评论