答案 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/