1

I have a tab bar which i have set up using storyboard. In the tab bar, i have a custom button in the middle which looks like:

enter image description here

It was set up using:

class TabBarViewController: UITabBarController {

let button = UIButton.init(type: .custom)

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    button.setImage(UIImage(named: "assetIcon"), for: .normal)
    button.backgroundColor = .blue
    button.layer.cornerRadius = 35
    button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)

    self.view.insertSubview(button, aboveSubview: self.tabBar)
    self.view.layoutIfNeeded()
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    let distance =  ((self.view.bounds.height)/100)*11

    // safe place to set the frame of button manually
    button.frame = CGRect.init(x: self.tabBar.center.x - 32, y: self.view.bounds.height - distance, width: 70, height: 70)


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@objc func buttonAction(){

       // no actions here yet as I dont know what to put
  }   
}

As seen above, i created an action for the button to navigate to a specific view like how other tab bar button should act. However, I am not sure how to navigate or rather should i say, I dont know what i should be putting in the custom tab bar button

Yogesh Patel
  • 1,893
  • 1
  • 20
  • 55
L.William
  • 382
  • 1
  • 4
  • 20
  • 1
    If you've set up your tab bar controller correctly you should just be able to do selectedIndex = 2 – Cameron Porter Jun 24 '19 at 02:39
  • sorry i didnt understand what u meant by selectedindex = 2-. Yes, tab bar is working correctly.It is the custom button, i do not know how to navigate using it – L.William Jun 24 '19 at 02:50
  • kindly read about UITabbarcontroller detail from apple documentation. – Muhammad Shauket Jun 24 '19 at 06:26
  • i already read...there was no issue with my tab bar controller..it's the custom button which i need to know how should i navigate to a specific View Controller as I am using storyboard to connect my tab bar previously – L.William Jun 24 '19 at 06:27
  • @L.William There is no need of custom button. You can achieve this kind of UI using bigger size image for centre TabBar item. This is the recommended way to implement TabBar, there are lots of example on StackOverFlow. Try this: https://stackoverflow.com/questions/30527738/how-do-we-create-a-bigger-center-uitabbar-item – Shiv Jaiswal Jun 24 '19 at 08:21

2 Answers2

0

I'm guessing you want to display a view controller using the button. Instantiate and display the View Controller using the following:

let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ViewControllerIdentifier")
self.present(controller, animated: true, completion: nil)
allanc
  • 31
  • 4
-1

When the button is tapped, I believe you need to be using the selectedViewController property to specify which view to display. Here's the documentation on the Tab Bar Controller: https://developer.apple.com/documentation/uikit/uitabbarcontroller

Ryan
  • 56
  • 1
  • 10
  • if is by adding the tab bar programatically, i think it is possible..but cause i am using storyboard previously, i dont know how to proceed – L.William Jun 24 '19 at 03:20