在没有切换选项卡的另一个选项卡的显示栏控制器中显示视图控制器

时间:2015-12-30 06:05:20

标签: ios swift uitabbarcontroller

我在Tab Bar Controller中嵌入了四个视图控制器(如下所示)。我想在Feed视图中显示配置文件视图,而无需切换选项卡。任何建议都会很棒。

enter image description here

1 个答案:

答案 0 :(得分:1)

最佳方法是添加子视图

  

这是我在Swift 2.0上测试的项目中使用的代码

 let tabBarController = self.sourceViewController as TabBarController
 let destinationController = self.destinationViewController as UIViewController

        for view in tabBarController.placeholderView.subviews as [UIView] {
            view.removeFromSuperview() // 1st remove from superview
        }

        // Add view to placeholder view
        tabBarController.currentViewController = destinationController
        tabBarController.placeholderView.addSubview(destinationController.view) // 2

        // Set autoresizing mask so it fits correctly
        tabBarController.placeholderView.setTranslatesAutoresizingMaskIntoConstraints(false)
        destinationController.view.setTranslatesAutoresizingMaskIntoConstraints(false)

        let horizontalConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v1]-0-|", options: .AlignAllTop, metrics: nil, views: ["v1": destinationController.view]) // 3

        tabBarController.placeholderView.addConstraints(horizontalConstraint)

        let verticalConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v1]-0-|", options: .AlignAllTop, metrics: nil, views: ["v1": destinationController.view]) // 3

        tabBarController.placeholderView.addConstraints(verticalConstraint)

        tabBarController.placeholderView.layoutIfNeeded() // 3
        destinationController.didMoveToParentViewController(tabBarController) // 4

    }

以下是我在做自定义tabbar时所引用的博客,希望它能为您提供帮助: http://swiftiostutorials.com/tutorial-custom-tabbar-storyboard/