我创建了一个自定义UIView,它充当我的应用程序的TabBar。 TabBar具有3个用作导航项的按钮,每按一次该项目,它的背景颜色就是我通过编程方式创建的另一个UIView。
它在iPhone X,XS,XS MAX和8 Plus上运行良好,在iPhone 8中出现问题。UIViewframe x大于应有的大小,我尝试对layoutIfNeeded和layoutSubviews进行操作均未成功。此外,在点击另一个TabBar项目并返回到已损坏的项目后,该问题已得到修复。
代码如下:
func tabBarSelected(selected: Bool, tabBarButton: UIButton) {
if selected {
let point = self.viewTabBar.convert(tabBarButton.frame, to: self.viewTabBar)
let backgroundView = UIView(frame: point)
backgroundView.tag = AppConstants.Keys.Defines.BackgroundViewTag
backgroundView.backgroundColor = .navigationBarBackgroundColor()
backgroundView.alpha = 0.0
backgroundView.layer.cornerRadius = 5.0
self.viewTabBar.addSubview(backgroundView)
self.viewTabBar.bringSubviewToFront(tabBarButton)
UIView.animate(withDuration: 0.2, animations: {
backgroundView.alpha = 0.5
}) { (complete) in
}
} else {
let backgroundView = self.viewTabBar.viewWithTag(AppConstants.Keys.Defines.BackgroundViewTag)
UIView.animate(withDuration: 0.2, animations: {
backgroundView?.alpha = 0.0
}) { (complete) in
backgroundView?.removeFromSuperview()
}
}
}
开始时帧看起来像这样:
DEBUG: Button frame: (306.0, 8.0, 100.0, 38.0)
然后按下另一个按钮并返回到特定项目:
DEBUG: Button frame: (276.5, 8.0, 90.5, 37.0)
问题出在哪里?
这是它的外观:
这是我的计划: