如何在标签栏上方底部的所有视图控制器中添加一个公共视图

How to add a common view in all view controllers at the bottom above tab bar

我需要添加一个具有一些按钮并出现在所有视图控制器中的视图。该视图应包含一个水平滚动视图,其中放置了所有按钮。

视图需要位于标签栏的顶部。与图片中的相似The player in the apple music application, on top of the tab bar in the bottom

感谢任何帮助。谢谢!

试试这个:

这是Swift 3 代码。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

    view.frame = CGRect(x: 0, y: UIScreen.main.bounds.height-100, width: UIScreen.main.bounds.width , height: 100)
    //window?.willRemoveSubview(view)

    //Add ScrollView to View
    let scrollview = UIScrollView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))
    scrollview.contentSize = CGSize( width: view.frame.size.width*2, height: view.frame.size.height)
    scrollview.backgroundColor = UIColor.blue
    scrollview.removeFromSuperview()
    view.addSubview(scrollview)

    //Add Button to Scrollview
    let btn = UIButton(frame: CGRect(x: scrollview.frame.size.width/3, y: scrollview.frame.size.height/2, width: 50, height: 30))
    btn.setTitle("Heloo", for: UIControlState.normal)
    btn.setTitleColor(UIColor.red, for: UIControlState.normal)
    let btn2 = UIButton(frame: CGRect(x: scrollview.frame.size.width/2, y: scrollview.frame.size.height/2, width: 50, height: 30))
    btn2.setTitle("Hiiii", for: UIControlState.normal)
    btn2.setTitleColor(UIColor.red, for: UIControlState.normal)
    scrollview.addSubview(btn)
    scrollview.addSubview(btn2)

    window?.makeKeyAndVisible()
    window?.insertSubview(view, at: 0)
    window?.bringSubview(toFront: view)

    return true
}

希望对您有所帮助

如果您需要放置视图,需要始终在 运行 应用程序的所有控制器视图之上显示,添加 subviewwindow 在您的 App 委托中。

优点:

  • 处理得当很快
  • 代码简洁

缺点:

  • 需要您创建自己的代表(负责操作的方法 带按钮)
  • 更努力地处理自动布局的约束
  • 如果设计工作很多
  • 很难处理这种情况,当您想 hide/show 那个栏,或者只是将它隐藏在某些视图控制器中时

或:

如果您只需要扩展具有滚动功能的 UITabBar,最好设置符合 tabbar 的 tabbar 委托并使用一些库对其进行扩展,例如:

https://github.com/kumapo/ScrollableTabs

使用库的优点:

  • 已经准备好的代表
  • 使用问题调试得很好

缺点:

  • 安装工程