ios - 在 UITabBar 上查看

标签 ios swift uitabbar

类似于播放歌曲时 Spotify 或 Apple Music 应用程序所做的,它将自定义 View 放置在 UITabBar 之上: enter image description here

我尝试过的解决方案:

  1. ViewController 中的 UITabBarController 具有最大尺寸的容器 View ,自定义 View 位于底部布局指南上方的容器 View49pt 顶部: enter image description here 问题:嵌入在 UITabBarController 中的 ViewController 中的任何内容都不会显示,因为它们隐藏在自定义布局后面。我尝试在 UITabBarController 中覆盖 size forChildContentContainer,尝试更新底部布局指南,Nothing。 我需要调整 UITabBarController 容器 View 的框架。

  2. 再次尝试 #1,但尝试通过 increasing the size of UITabBar 解决隐藏在它后面的内容的问题, 然后使用 ImageInset on every TabBarItem将其关闭,并在 UITabBar 顶部添加我的自定义 View 。效果不是很好。有时我想隐藏我的自定义 View 。

  3. UITabBarController 作为根,每个 child 都是一个 ViewController,带有容器 View + 我的自定义 View : enter image description here 但是现在我有多个自定义 View 实例在四处 float 。如果我想更改它上面的标签,必须将其更改为所有 View 。或者隐藏等

  4. 覆盖 UITabBarController 的 UITabBar 属性并返回我的自定义 UITabBar(用 xib 对其进行膨胀),它有一个 UITabBar + 我的自定义 View 。 问题:可能是所有尝试中最令人沮丧的尝试。如果您使用 class MyCustomTabBar : UITabBar {} 的实例覆盖该属性,则不会显示任何选项卡!是的,我将 myCustomTabBar 的委托(delegate)设置为 self

倾向于 #3,但正在寻找更好的解决方案。

最佳答案

如果您继承 UITabBarController 并以编程方式添加您的 View ,这实际上非常容易。使用此技术自动支持标签栏的旋转和大小更改,无论您使用的是哪个版本。

class CustomTabBarController: UITabBarController {
  override func viewDidLoad() {
    super.viewDidLoad()

    //...do some of your custom setup work
    // add a container view above the tabBar
    let containerView = UIView()
    containerView.backgroundColor = .red
    view.addSubview(containerView)
    containerView.translatesAutoresizingMaskIntoConstraints = false
    containerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    containerView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true

    // anchor your view right above the tabBar
    containerView.bottomAnchor.constraint(equalTo: tabBar.topAnchor).isActive = true

    containerView.heightAnchor.constraint(equalToConstant: 50).isActive = true
  }
}

关于ios - 在 UITabBar 上查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42384470/

相关文章:

iOS:将行添加到 TableView

swift - 怎么查字典?

ios - UITabBar 边框和阴影问题

objective-c - 为什么我在标签栏点击时出现构建错误?

ios - 在多个 Collection View 中重用单个 UICollectionViewCell

ios - 结合 UIView 和 UITableView

iphone - 如何在 iphone 应用程序中用饼图显示百分比

ios - 试图使同一索引路径的多个单元格出队,这是不允许的。错误

ios - 如何在从 UITableViewController 到 UIViewController 的 Unwind segue 中声明 UITableViewController

swift - 覆盖自定义 TabBar 的属性?