tHatpart
tHatpart

Reputation: 1146

Swift Key window subview disappearing

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.

Upvotes: 2

Views: 169

Answers (0)

Related Questions