Swift 如何在选项卡栏上方底部的所有视图控制器中添加公共视图

Swift 如何在选项卡栏上方底部的所有视图控制器中添加公共视图,swift,xcode,swift2,Swift,Xcode,Swift2,我需要添加一个视图,该视图具有一些按钮,并显示在所有视图控制器中。该视图应包含一个水平滚动视图,其中放置了所有按钮 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization afte

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

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
}
视图需要位于选项卡栏的顶部。与图中的相似

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
}
感谢您的帮助。谢谢

试试这个:

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
}
这是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
}

如果您需要在运行应用程序的所有时间将需要显示在所有控制器视图顶部的视图放置在应用程序代理的窗口中,希望获得此帮助

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
}
优点:

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
}
  • 如果做得恰当,速度很快
  • 代码的简洁性
缺点:

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
}
  • 需要您使自己的委托(方法)对操作负责 (带按钮)
  • 使用自动布局的约束进行更困难的工作
  • 如果设计工作
  • 当您想要隐藏/显示该栏,或者只是在某些视图控制器中隐藏该栏时,请务必处理此情况
或:

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
}
如果您只需要扩展具有滚动功能的UITabBar,最好设置符合tabbar的tabbar委托,并使用一些库进行扩展,例如:

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
}

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
}
使用库的优点:

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
}
  • 已经准备好的代表
  • 使用方面的问题都经过了很好的调试
缺点:

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
}
  • 安装工程

非常感谢。它起作用了,但我如何才能在某些视图控制器中隐藏此视图?谢谢您回答我的问题。我知道很难处理视图应该隐藏在某些视图控制器中的情况,但如果您对如何处理这种情况有任何想法,请告诉我。您的问题已被搁置,因此根据规则,您应该尽可能详细说明您想要实现的目标,以及发布您首先尝试的内容。我本想帮你的,但规则很重要。