Search code examples
iosswiftuitabbarcontrolleruitabbar

adding a button programatiy to UITabBar is moved when dismissed a modal ViewController


I'm adding a button programaticly to TabBarController using this code:

    let tabBarHeight = tabBar.layer.bounds.height * 1.2
    let win: UIWindow = ((UIApplication.shared.delegate?.window)!)!
     let button = Floaty(frame: CGRect(origin: CGPoint(x: 0.0, y: win.frame.size.height),size: CGSize(width: tabBarHeight, height: tabBarHeight)))
     button.center = CGPoint(x: win.center.x, y: win.frame.size.height - tabBar.layer.bounds.height)
     button.buttonImage = UIImage(named: "icoZ")
    let item = FloatyItem()
    item.buttonColor = UIColor(named: "ButtonGreen") ?? UIColor.green
    item.iconImageView.image = #imageLiteral(resourceName: "percentButton")
    item.handler = { item in
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "PercentVC") ?? PercentViewController()
        DispatchQueue.main.async {
            self.present(vc, animated: true, completion: nil)
            button.close()
        }
    }
    button.addItem(item: item)
    let item2 = FloatyItem()
    item2.buttonColor = UIColor(named: "ButtonGreen") ?? UIColor.green
    item2.iconImageView.image = #imageLiteral(resourceName: "scanQr")
    item2.handler = { item in
        let vc = ScannerViewController()
       DispatchQueue.main.async {
           self.present(vc, animated: true, completion: nil)
           button.close()
       }
    }
    button.addItem(item: item2)
    self.view.addSubview(button)
    button.translatesAutoresizingMaskIntoConstraints = false
    let bottom = button.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -15)
    let center = button.centerXAnchor.constraint(equalTo: view.centerXAnchor)
    let height = button.heightAnchor.constraint(equalToConstant: tabBarHeight)
    let width = button.widthAnchor.constraint(equalToConstant: tabBarHeight)
    NSLayoutConstraint.activate([bottom,center,height,width])

but when I present a modal view controller and dismiss it the button is moved to the middle of the screen. like this. enter image description here the desired is this.enter image description here


Solution

  • A quick workaround may be to change your modal presentation style to be overFullScreen. The default modal presentation style removes your view from the hierarchy and re-adds it when the modal is dismissed. That causes your views to re-layout.

    If that's not acceptable, start with Xcode's view debugger. I'm guessing that the parent view for the button isn't being laid out correctly. If it is being laid out correctly you can try call setNeedsLayout() on the view in viewWillAppear to trigger a recalculation of the layout.

    Finally, what you're doing isn't really best practice. You have no guarantee that the tab bar won't be moved above your button the next time the UITabBarController triggers a layout. This honestly should probably be a custom component if you're concerned about long term stability.