1

I want to mimic Instagram's tabbar functionality, where the middle option acts as a button that triggers a modal slide up segue instead of switching views like a normal tabbar button.

I don't know the correct way of doing this, but I'm trying to place a button directly over the middle tabbar button, but when pressing it it triggers the tabbar button instead of the normal button.

    let count = CGFloat(tabBar.items!.count)
    let itemSize = CGSize(width: tabBar.frame.size.width / count, height: tabBar.frame.height)

    for (index, _) in tabBar.items!.enumerate() {
        if index == 2 {
            let xPosition = itemSize.width * CGFloat(index)

            let backgroundButton = UIButton(frame: CGRect.init(x: xPosition, y: 0, width: itemSize.width, height: itemSize.height))

            backgroundButton.backgroundColor = UIColor.redColor()

            backgroundButton.addTarget(self, action: #selector(MainTabBarController.testButton), forControlEvents: .TouchUpInside)
           //testButton just triggers a print statement for now

            tabBar.insertSubview(backgroundButton, atIndex: 1)
        }
    }

When I press the middle button, it just triggers the normal view switching like with any other tabbar button, and does not print anything out meaning the overlayed button is never trigger.

Is there somewhere I went wrong, or better yet is there a better way of doing this? Prehaps making the tabbar button itself trigger a modal segue instead of the typical tabbar button behavior?

Tommy K
  • 1,759
  • 3
  • 28
  • 51

0 Answers0