[英]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 触发布局时标签栏不会移动到按钮上方。 如果您担心长期稳定性,老实说这应该是一个自定义组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.