我目前正在使用SWRevealViewController作为我的应用中的侧边栏菜单。当我单击其中一个选项时,目标视图控制器没有“后退”按钮,因为它不是来自正确的视图控制器(即要返回的页面)。
因此我想在目标视图控制器上手动创建一个后退按钮,它将返回到主视图控制器。
我在这里查看了代码:How do I manually set the "Back" destination in iOS apps
但我正在努力在Swift中实现这个(一个接一个错误!)。有帮助吗?谢谢!
修改
我已尝试过以下建议,但后退按钮却没有出现。这可能与我在其他视图中隐藏导航栏并在目标视图上执行以下操作有关:
override func viewDidLoad() {
super.viewDidLoad()
navigationController.setNavigationBarHidden(false, animated:true)
var myBackButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
myBackButton.addTarget(self, action: "popToRoot:", forControlEvents: UIControlEvents.TouchUpInside)
var myCustomBackButtonItem:UIBarButtonItem = UIBarButtonItem(customView: myBackButton)
self.navigationItem.leftBarButtonItem = myCustomBackButtonItem
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func popToRoot(sender:UIBarButtonItem){
self.navigationController.popToRootViewControllerAnimated(true)
}
不确定为什么后退按钮不会出现?
修改
这是我的侧边栏视图控制器中的prepareForSegue。如果有办法检查segue标识符'test',那么我可以从这里设置后退按钮吗?
- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender
{
// Set the title of navigation bar by using the menu items
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController;
destViewController.title = [[_menuItems objectAtIndex:indexPath.row] capitalizedString];
if ( [segue isKindOfClass: [SWRevealViewControllerSegue class]] ) {
SWRevealViewControllerSegue *swSegue = (SWRevealViewControllerSegue*) segue;
swSegue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc) {
UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
[navController setViewControllers: @[dvc] animated: NO ];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
};
}
}
答案 0 :(得分:25)
你可以像这样快速写出来
将此内容写入navigationController
navigationController.setNavigationBarHidden(false, animated:true)
var myBackButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
myBackButton.addTarget(self, action: "popToRoot:", forControlEvents: UIControlEvents.TouchUpInside)
myBackButton.setTitle("YOUR TITLE", forState: UIControlState.Normal)
myBackButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
myBackButton.sizeToFit()
var myCustomBackButtonItem:UIBarButtonItem = UIBarButtonItem(customView: myBackButton)
self.navigationItem.leftBarButtonItem = myCustomBackButtonItem
这将弹出rootViewController
func popToRoot(sender:UIBarButtonItem){
self.navigationController.popToRootViewControllerAnimated(true)
}
答案 1 :(得分:5)
在 Swift 2.1
中我以这种方式自定义导航栏项
在AppDelegate 我有
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
//Cutomize navigation bar
let navBgImage:UIImage = UIImage(named: "dummy_navigation_bg_image")!
UINavigationBar.appearance().setBackgroundImage(navBgImage, forBarMetrics: .Default)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()];
return true
}
在ViewController中我以这种方式更新viewWillAppear方法中的导航项
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
setupNavigationBar()
}
// MARK: - NavigationBar methods
func setupNavigationBar ()
{
//current tab screen title
tabBarController?.title = "Manage"
//Hide back button or left bar button
tabBarController?.navigationItem.leftBarButtonItem = UIBarButtonItem(title: " ", style: .Plain, target: nil, action: nil)
//custom right bar button
var image = UIImage(named: "dummy_settings_icon")
image = image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
tabBarController?.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: self, action: "settingButtonAction")
}
func settingButtonAction()
{
print("Setting Button tapped")
performSegueWithIdentifier("loadAccountSettingsSegue", sender: nil)
}
答案 2 :(得分:5)
Swift 3.0 解决方案:
self.navigationController?.setNavigationBarHidden(false, animated:false)
//Create back button of type custom
let myBackButton:UIButton = UIButton.init(type: .custom)
myBackButton.addTarget(self, action: #selector(ViewController.popToRoot(sender:)), for: .touchUpInside)
myBackButton.setTitle("Back".localized(), for: .normal)
myBackButton.setTitleColor(.blue, for: .normal)
myBackButton.sizeToFit()
//Add back button to navigationBar as left Button
let myCustomBackButtonItem:UIBarButtonItem = UIBarButtonItem(customView: myBackButton)
self.navigationItem.leftBarButtonItem = myCustomBackButtonItem
func popToRoot(sender:UIBarButtonItem){
_ = self.navigationController?.popToRootViewController(animated: true)
}
答案 3 :(得分:3)
这是Swift 2 / 2.1的更新版本:
self.navigationController? .setNavigationBarHidden(false, animated:true)
let backButton = UIButton(type: UIButtonType.Custom)
backButton.addTarget(self, action: "methodCall:", forControlEvents: UIControlEvents.TouchUpInside)
backButton.setTitle("TITLE", forState: UIControlState.Normal)
backButton.sizeToFit()
let backButtonItem = UIBarButtonItem(customView: backButton)
self.navigationItem.leftBarButtonItem = backButtonItem
答案 4 :(得分:0)
Swift 5 / XCode 11.4
override func viewDidLoad() {
super.viewDidLoad()
navigationController!.setNavigationBarHidden(false, animated: true)
let myBackButton:UIButton = UIButton(type: UIButton.ButtonType.custom) as UIButton
myBackButton.addTarget(self, action: #selector(popToRoot), for: UIControl.Event.touchUpInside)
myBackButton.setTitle("YOUR TITLE", for: UIControl.State.normal)
myBackButton.setTitleColor(UIColor.white, for: UIControl.State.normal)
myBackButton.sizeToFit()
let myCustomBackButtonItem:UIBarButtonItem = UIBarButtonItem(customView: myBackButton)
self.navigationItem.leftBarButtonItem = myCustomBackButtonItem
}
此外,您应该将popToRoot标记为@objc
@objc func popToRoot() {
//enter action here
}