[英]Present view controller while current view controller has a UITabBar but no UITabBarController
I have a UIViewController
with a UITabBar
at top of it. 我有一个
UIViewController
在它的顶部带有UITabBar
。 I am not using a UITabBarController
because I need the UITabBar
at the top and not at the bottom of the view. 我没有使用
UITabBarController
因为我需要在视图的顶部而不是底部的UITabBar
。 Here is my current view controller : 这是我当前的视图控制器:
class CustomTabBarController: UIViewController, UITabBarDelegate
{
@IBOutlet weak var tabBar: UITabBar!
var viewControllers: [UIViewController] = []
//MARK: UIVIewController delegate
override func viewDidLoad()
{
super.viewDidLoad()
//TabBarController
let storyboard = UIStoryboard(name: "Main", bundle: nil)
self.viewControllers.append(storyboard.instantiateViewControllerWithIdentifier("myWorldViewController") as UIViewController)
self.viewControllers.append(storyboard.instantiateViewControllerWithIdentifier("meViewController") as UIViewController)
self.viewControllers.append(storyboard.instantiateViewControllerWithIdentifier("welcomeViewController") as UIViewController)
self.tabBar.selectedItem = self.tabBar.items?.first
//Graphical Settings
let itemWidth = self.tabBar.frame.width / 3 as CGFloat
let image = self.imageWithColor(UIColor.grayColor(), size: CGSizeMake(itemWidth, 49))
UITabBar.appearance().barTintColor = UIColor.whiteColor()
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName:UIFont(name: "Helvetica Neue", size: 20)!], forState: UIControlState.Normal)
UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -10)
UITabBar.appearance().tintColor = UIColor.whiteColor()
UITabBar.appearance().selectionIndicatorImage = image
//Borders
self.tabBar.layer.borderColor = UIColor.grayColor().CGColor
self.tabBar.layer.borderWidth = 1
self.tabBar(self.tabBar, didSelectItem: (self.tabBar.items?.first)!)
}
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem)
{
self.view.insertSubview(self.viewControllers[item.tag].view, belowSubview: self.tabBar)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
//MARK: Utils
func imageWithColor(color: UIColor, size: CGSize) -> UIImage
{
let rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContext(size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect )
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
What I need now, it to present a view controller when clicking on a button in one of the tab views. 我现在需要的是,当单击选项卡视图之一中的按钮时,它会显示一个视图控制器。 On the destination controller, I need to keep the
UITabBar
(like when using a normal UITabBarController
). 在目标控制器上,我需要保留
UITabBar
(就像使用普通的UITabBarController
)。 How can I do this ? 我怎样才能做到这一点 ?
You want to create a custom Container View Controller . 您要创建一个自定义的Container View Controller 。 Your
CustomTabBarController
should be the parent view controller of the view controllers for each tab. 您的
CustomTabBarController
应该是每个选项卡的视图控制器的父视图控制器。 It will be the one responsible for displaying a UITabBar
and the view of the selected child view controller below. 它是负责显示
UITabBar
和下面的所选子视图控制器的视图的UITabBar
。
Try incorporating the code below into your class: 尝试将以下代码合并到您的班级中:
let contentView = UIView()
var selectedViewController: UIViewController?
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(contentView)
// ... rest of your code
// + layout of contentView to take up the area below tabBar
}
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
if let currentVC = selectedViewController {
currentVC.willMoveToParentViewController(nil)
currentVC.view.removeFromSuperview()
currentVC.removeFromParentViewController()
}
let newVC = viewControllers[item.tag]
addChildViewController(newVC)
contentView.addSubview(newVC.view)
newVC.view.frame = contentView.bounds
newVC.didMoveToParentViewController(self)
selectedViewController = newVC
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.