取消模态时,向 UITabBar 添加按钮程序会被移动 ViewController

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

我正在使用以下代码以编程方式向 TabBarController 添加一个按钮:

    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])

但是当我呈现模态视图控制器并将其关闭时,按钮会移动到屏幕中间。像这样。 想要的是这个

一个快速的解决方法可能是将模式显示样式更改为 overFullScreen。默认模态呈现样式会从层次结构中删除您的视图,并在模态消失时重新添加它。这会导致您的视图重新布局。

如果不能接受,请从 Xcode 的视图调试器开始。我猜按钮的父视图布局不正确。如果布局正确,您可以尝试在 viewWillAppear 中的视图上调用 setNeedsLayout() 以触发重新计算布局。

最后,您所做的并不是真正的最佳实践。您无法保证下次 UITabBarController 触发布局时标签栏不会移动到您的按钮上方。如果您担心长期稳定性,老实说,这可能应该是一个自定义组件。