Hiding an overlayed button on a Tab Bar

209 Views Asked by At

I am newbie in iOS and Swift. I am currently working on an application where I am required to show a tab bar with a big button in the center. Given the time constraints, what I have done is I created a button in the Window and positioned it on top of the Tab Bar programatically. Now when I navigate to or away from this screen, I am adding / removing this button in viewDidAppear and viewDidDisappear respectively. This makes sure that as tab bar goes away the button is also not shown and similarly when tab bar is shown the button is also added on top. However, because the addition and removal happens in viewDidAppear and viewDidDisappear, there is a slight delay in rendering and removing of the button because of which a momentary flickr is seen. Doing the same in viewWillAppear and viewWillDisappear doesn't work at all. The button doesn't show up or gets hidden in 'will' methods. Can someone please suggest what probably would be going wrong here? Thanks in advance. I am attaching a screen shot to give a rough idea about how it is supposed to look.

required ui

1

There are 1 best solutions below

0
On

Proper way to add button is add it in view of UItabBarController's view instead adding in UIWindow

class DashBoardViewController: UITabBarController {

    let button:UIButton = {
        let view = UIButton(frame: .zero)
        view.backgroundColor = .blue
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        initView()
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        button.center = tabBar.center
    }

    private func initView() {
        button.center = tabBar.center
        view.addSubview(button)
    }
}