gpt4 book ai didi

Swift - 从滑入式菜单中打开 UIViewController

转载 作者:行者123 更新时间:2023-11-30 12:29:29 24 4
gpt4 key购买 nike

我有一个滑入式菜单,当我按下导航栏中的菜单按钮时,它会自动显示。可以通过点击背景或菜单本身来关闭它。在此 View 中,我有几个项目,其中之一是用户配置文件按钮。当我按下此按钮时,我希望菜单自行消失,然后立即打开用户配置文件 View Controller 。这就是为什么我先调用handleDismiss(),然后设置需要打开的 View

但是,它一直告诉我该 View 不在窗口层次结构中。我知道问题是什么(查看堆栈),但我不知何故无法让它正常工作。我该如何解决这个问题?

RootViewController -> HomeController(tabBar 索引 0) -> 滑入式菜单 -> UserProfileController

打开 Controller 的按钮

profileCell.profileButton.addTarget(self, action: #selector(handleUserProfile(sender:)), for: .touchUpInside)

函数

func handleDismiss() {
UIView.animate(withDuration: 0.5) {
self.blackView.alpha = 0

if let window = UIApplication.shared.keyWindow {
self.collectionView.frame = CGRect(x: 0, y: window.frame.height, width: self.collectionView.frame.width, height: self.collectionView.frame.height)
}
}
}

func handleUserProfile(sender: UIButton) {
handleDismiss()

let userProfileController = UserProfileController()
openProfileController(userProfileController)
}

func openProfileController(_ controller: UIViewController) {
present(controller, animated: false, completion: nil)
}

最佳答案

我通过在菜单中设置对 rootViewController 的引用解决了该问题。

家庭 Controller

let menuLauncher = MenuLauncher()

func tappedMenu(sender: UIButton) {
menuLauncher.showMenu(self)
}

菜单 Controller

func MenuLauncher() {
self.m_ParentViewController = nil
}

func showMenu(_ parentViewController: UIViewController) {
self.m_ParentViewController = parentViewController

collectionView.dataSource = self
collectionView.delegate = self

collectionView.register(MenuLauncherImageCell.self, forCellWithReuseIdentifier: cellIdImage)
collectionView.register(MenuLauncherInfoCell.self, forCellWithReuseIdentifier: cellIdInfo)

if let window = UIApplication.shared.keyWindow {
blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)

blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))

window.addSubview(blackView)
window.addSubview(collectionView)

collectionView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: 350)

blackView.frame = window.frame
blackView.alpha = 0

UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.blackView.alpha = 1
self.collectionView.frame = CGRect(x: 0, y: window.frame.height - 350, width: self.collectionView.frame.width, height: self.collectionView.frame.height)

}, completion: nil)
}
}

func handleDismiss() {
UIView.animate(withDuration: 0.5) {
self.blackView.alpha = 0

if let window = UIApplication.shared.keyWindow {
self.collectionView.frame = CGRect(x: 0, y: window.frame.height, width: self.collectionView.frame.width, height: self.collectionView.frame.height)
}
}
}

func handleUserProfile(sender: UIButton) {
handleDismiss()

let userProfileController = UserProfileController()
self.m_ParentViewController?.present(userProfileController, animated: true, completion: nil)
}

关于Swift - 从滑入式菜单中打开 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43762121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com