简体   繁体   English

关闭模式 ViewController 时,向 UITabBar 添加按钮程序会被移动

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

I'm adding a button programaticly to TabBarController using this code:我正在使用以下代码以编程方式向 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])

but when I present a modal view controller and dismiss it the button is moved to the middle of the screen.但是当我展示一个模态视图控制器并关闭它时,按钮被移动到屏幕中间。 like this.像这样。 在此处输入图片说明 the desired is this.想要的是这个。 在此处输入图片说明

A quick workaround may be to change your modal presentation style to be overFullScreen .一个快速的解决方法可能是将您的模态演示样式更改为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.如果这是不可接受的,请从 Xcode 的视图调试器开始。 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.如果布局正确,您可以尝试在viewWillAppear的视图上调用setNeedsLayout()以触​​发重新计算布局。

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.您无法保证下次 UITabBarController 触发布局时标签栏不会移动到按钮上方。 This honestly should probably be a custom component if you're concerned about long term stability.如果您担心长期稳定性,老实说这应该是一个自定义组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM