【问题标题】:Tab Bar overlap view behind后面的标签栏重叠视图
【发布时间】:2018-11-05 07:38:52
【问题描述】:

我似乎无法解决这个问题,我将标签栏高度从 viewWillLayoutSubviews() 调整为 60,但覆盖视图似乎不承认调整后的高度并效仿。

我发现的其他类似问题实际上并不相似(参见此处:iOS 7 Custom TableView Is Under TabBar),因为它们的标签栏是半透明的,而我的不是。

这是我目前实现的:

在我的自定义UITabBarController

override func viewWillLayoutSubviews() {
    var newTabBarFrame = tabBar.frame
    let newTabBarHeight: CGFloat = 60
    newTabBarFrame.size.height = newTabBarHeight
    newTabBarFrame.origin.y = self.view.frame.size.height - newTabBarHeight
    tabBar.frame = newTabBarFrame
}

在我的标签之一UIViewController

override func viewDidLoad() {
    view.addSubview(tableView)
    NSLayoutConstraint.activate([
        NSLayoutConstraint(item: tableView, attribute: NSLayoutAttribute.leading, relatedBy: .equal, toItem: view, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0),
        NSLayoutConstraint( item: tableView, attribute: NSLayoutAttribute.top, relatedBy: .equal, toItem: view, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0),
        NSLayoutConstraint(item: tableView, attribute: NSLayoutAttribute.trailing, relatedBy: .equal, toItem: view, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0),
        NSLayoutConstraint(item: tableView, attribute: NSLayoutAttribute.bottom, relatedBy: .equal, toItem: view, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)
        ])
}

这是当前结果: 可以看到覆盖视图被部分遮挡。这发生在所有其他选项卡的覆盖视图控制器上

顺便说一句,我已经确保 tableview 的 translatesAutoresizingMaskIntoConstraints 设置为 false

【问题讨论】:

    标签: ios swift autolayout


    【解决方案1】:

    您可以使用自定义UITabBar 来执行此操作。只需覆盖 sizeThatFits(_:) 即可使用您的自定义高度:

    class TabBar: UITabBar {
    
        private let height:CGFloat = 60
    
        override func sizeThatFits(_ size: CGSize) -> CGSize {
            var bottomSafeAreaInsets: CGFloat = 0
            if #available(iOS 11.0, *) {
                bottomSafeAreaInsets = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
            }
            return CGSize(width: size.width, height: height + bottomSafeAreaInsets)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多