在不显示选项卡栏的情况下执行到另一个导航控制器的转场
Perform segue to another Navigation Controller without showing Tab Bar
我有一个根 Tab Host Controller
,它有两个 Navigation Controller
选项卡兄弟姐妹:(1) Nearby Stops 和 (2) Saved Stops。其中每一个都有一个 View Controller
分别。
我想执行从其中一个兄弟 View Controllers
到另一个 Navigation Controller
的 segue,其中嵌入了 Stop Schedule View Controller,并满足以下要求:
- 根
Tab Bar
不应显示在此 View Controller
的底部
- 我需要在执行 segue 之前将
Stop
对象传递给此视图控制器
故事板:
目前,我正在以这种方式执行 segue,尽管 Tab Bar
在不应该保留在 Stop Schedule View Controller 上。
func showStopSchedule(stop: Stop) {
let stopScheduleController = self.storyboard?.instantiateViewControllerWithIdentifier("StopScheduleViewController") as! StopScheduleViewController
stopScheduleController.stop = stop // pass data object
self.navigationController?.pushViewController(stopScheduleController, animated: true)
}
您没有使用刚刚在 Storyboard 中定义的 segue。相反,您目前正在手动重新加载 StopScheduleViewController
,而您应该只执行您已经定义的 segue
。
将 标识符 添加到每个要以编程方式调用的 Storyboard Segue,
然后以这种方式加载它们:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("showStopSchedule", sender: self)
}
您可以在显示停止计划视图控制器时简单地设置标签栏的 hidden
属性,并在该视图控制器消失之前取消隐藏标签栏
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.hidden=true
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.tabBarController?.tabBar.hidden=false
}
更新:要使过渡动画化,您可以使用此方法:
class StopViewController: UIViewController {
var barFrame:CGRect?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// self.tabBarController?.tabBar.hidden=true
if let tabBar=self.tabBarController?.tabBar {
self.barFrame=tabBar.frame
UIView.animateWithDuration(0.3, animations: { () -> Void in
let newBarFrame=CGRectMake(self.barFrame!.origin.x, self.view.frame.size.height, self.barFrame!.size.width, self.barFrame!.size.height)
tabBar.frame=newBarFrame
}, completion: { (Bool) -> Void in
tabBar.hidden=true
})
}
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.tabBarController?.tabBar.hidden=false;
if self.barFrame != nil {
UIView.animateWithDuration(0.3, animations: { () -> Void in
let newBarFrame=CGRectMake(self.barFrame!.origin.x, self.view.frame.size.height-self.barFrame!.size.height, self.view.frame.size.width, self.barFrame!.size.height)
self.tabBarController?.tabBar.frame=newBarFrame
})
}
}
}
如果您只想隐藏 navigationController,下面的代码有效。
self.navigationController?.navigationBar.hidden = true
我有一个根 Tab Host Controller
,它有两个 Navigation Controller
选项卡兄弟姐妹:(1) Nearby Stops 和 (2) Saved Stops。其中每一个都有一个 View Controller
分别。
我想执行从其中一个兄弟 View Controllers
到另一个 Navigation Controller
的 segue,其中嵌入了 Stop Schedule View Controller,并满足以下要求:
- 根
Tab Bar
不应显示在此View Controller
的底部
- 我需要在执行 segue 之前将
Stop
对象传递给此视图控制器
故事板:
目前,我正在以这种方式执行 segue,尽管 Tab Bar
在不应该保留在 Stop Schedule View Controller 上。
func showStopSchedule(stop: Stop) {
let stopScheduleController = self.storyboard?.instantiateViewControllerWithIdentifier("StopScheduleViewController") as! StopScheduleViewController
stopScheduleController.stop = stop // pass data object
self.navigationController?.pushViewController(stopScheduleController, animated: true)
}
您没有使用刚刚在 Storyboard 中定义的 segue。相反,您目前正在手动重新加载 StopScheduleViewController
,而您应该只执行您已经定义的 segue
。
将 标识符 添加到每个要以编程方式调用的 Storyboard Segue,
然后以这种方式加载它们:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("showStopSchedule", sender: self)
}
您可以在显示停止计划视图控制器时简单地设置标签栏的 hidden
属性,并在该视图控制器消失之前取消隐藏标签栏
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.hidden=true
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.tabBarController?.tabBar.hidden=false
}
更新:要使过渡动画化,您可以使用此方法:
class StopViewController: UIViewController {
var barFrame:CGRect?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// self.tabBarController?.tabBar.hidden=true
if let tabBar=self.tabBarController?.tabBar {
self.barFrame=tabBar.frame
UIView.animateWithDuration(0.3, animations: { () -> Void in
let newBarFrame=CGRectMake(self.barFrame!.origin.x, self.view.frame.size.height, self.barFrame!.size.width, self.barFrame!.size.height)
tabBar.frame=newBarFrame
}, completion: { (Bool) -> Void in
tabBar.hidden=true
})
}
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.tabBarController?.tabBar.hidden=false;
if self.barFrame != nil {
UIView.animateWithDuration(0.3, animations: { () -> Void in
let newBarFrame=CGRectMake(self.barFrame!.origin.x, self.view.frame.size.height-self.barFrame!.size.height, self.view.frame.size.width, self.barFrame!.size.height)
self.tabBarController?.tabBar.frame=newBarFrame
})
}
}
}
如果您只想隐藏 navigationController,下面的代码有效。
self.navigationController?.navigationBar.hidden = true