Shravya Shravya - 2 months ago 13
Swift Question

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

I need to add a view that has some buttons and appears in all the view controllers. The view should contain a horizontal scrollview in which all the buttons are placed.

The view needs to be on top of the tab bar. Similar to the one in the imageThe player in the apple music application, on top of the tab bar in the bottom

Any help is appreciated. Thanks!

Answer

Try this:

This is Swift 3 code.

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
}

Hope this help

Comments