在视图控制器之间导航的自定义选项卡栏

时间:2019-06-24 02:23:21

标签: ios swift uibutton uitabbarcontroller uinavigationbar

我有一个使用情节提要设置的标签栏。在标签栏中,我在中间有一个自定义按钮,如下所示:

enter image description here

它是通过以下方式设置的:

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

如上所述,我为按钮创建了一个动作,以导航到特定视图,如其他选项卡栏按钮应如何动作。但是,我不确定如何导航,或者我应该说,我不知道我应该在自定义标签栏按钮中放什么

2 个答案:

答案 0 :(得分:0)

我猜您想使用该按钮显示视图控制器。 使用以下方法实例化并显示View Controller:

let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ViewControllerIdentifier")
self.present(controller, animated: true, completion: nil)

答案 1 :(得分:-1)

点击按钮时,我相信您需要使用selectedViewController属性来指定要显示的视图。以下是标签栏控制器上的文档:https://developer.apple.com/documentation/uikit/uitabbarcontroller