Swift Key 窗口子视图消失

发布于 2025-01-10 22:03:05 字数 1400 浏览 3 评论 0原文

我尝试在应用程序中实现的按钮遇到一些奇怪的行为。我有一个“撰写帖子”按钮,我将其添加到应用程序的关键窗口中。我想创建按钮并在某些情况下隐藏它,例如当导航控制器推送新的视图控制器时(即单击帖子)。该应用程序的设置如下:

Main 是一个故事板,它是 MainTabViewController 类型的自定义 UITabController,有 4 个选项卡,每个选项卡都有一个 MainTabViewController 类型的 UINavigationController 容器。代码>BaseViewController。

MainTabViewControllerviewDidLoad 函数中,我创建了撰写按钮并将其作为子视图添加到关键窗口,如下所示:

 func createButton(){
        let keyWindow = UIApplication.shared.keyWindow
        postButtonContainer = FanMenu...
        keyWindow?.addSubview(postButtonContainer)
 }
    

BaseViewController 中,我使用viewWillDisappear 隐藏撰写按钮(即单击帖子时),然后 viewWillAppear 通过设置显示撰写按钮isHidden 为 true 或 false。

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    ComposeButtonManager.shared.hidePostButton()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    ComposeButtonManager.shared.showPostButton()
}

仅当我从 Xcode 启动后崩溃关闭应用程序后,才会出现此问题。发生的情况是,当我在崩溃关闭后重新打开时 - 应用程序将正常加载并显示撰写按钮,但如果我从选项卡导航然后返回到同一选项卡,则不会显示撰写按钮。如果我单击帖子并导航回 BaseViewController 子类,该按钮将重新出现。我不确定为什么按钮在选项卡栏切换后隐藏,当所有 UIViewController 调用以在视图出现时取消隐藏按钮时。

当我从 Xcode 在手机上运行该项目时,我可以切换选项卡,并且按钮不会消失,它仅在我崩溃关闭并断开与 Xcode 的连接后才会发生,不确定为什么会发生这种情况。

I am experiencing some weird behavior with a button I am trying to implement in my app. I have a 'compose post' button that I am adding to the key window of the app. I want to create the button and hide it in certain scenarios like when the navigation controller pushes a new view controller (i.e. clicking on a post). The app is setup like this:

Main is a storyboard that is a custom UITabController of type MainTabViewController that has 4 tabs, each a UINavigationController that has a container that is of type BaseViewController.

In the viewDidLoad function of MainTabViewController I create the compose button and add it to the key window as a subview like this:

 func createButton(){
        let keyWindow = UIApplication.shared.keyWindow
        postButtonContainer = FanMenu...
        keyWindow?.addSubview(postButtonContainer)
 }
    

And in BaseViewController I use viewWillDisappear to hide the compose button (i.e. when post is clicked on) and then viewWillAppear to show the compose button by setting the isHidden to true or false.

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    ComposeButtonManager.shared.hidePostButton()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    ComposeButtonManager.shared.showPostButton()
}

The issue is happening only after I crash close the app after booting up from Xcode. What's happening is that when I reopen after crash closing - the app will load fine and present the compose button, but if I navigate from a tab and then back to that same tab, the compose button is not showing. And if I click a post and navigate back to BaseViewController subclass the button will re appear. I am not sure why the button is hiding after the tab bar switches, when all the UIViewControllers call to unhide the button when their view will appear.

When I run the project on my phone from Xcode, I can switch around tabs and the button won't disappear, its only happening after I crash close and disconnect from Xcode, not sure why this is happening.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文