ios 如何隐藏 UINavigationController 中的“后退”按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1453519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:25:47  来源:igfitidea点击:

How to hide the "back" button in UINavigationController?

iosiphoneuinavigationcontroller

提问by Zoran Simic

Do you know how to hide the 'back' button in a UINavigationController? Also, how to show it back, but I guess that's very similar to hiding it...

您知道如何隐藏 UINavigationController 中的“后退”按钮吗?另外,如何显示它,但我想这与隐藏它非常相似......

Just like the mail application does on the iPhone when you hit 'Edit' while viewing emails.

就像 iPhone 上的邮件应用程序在您查看电子邮件时点击“编辑”一样。

回答by Zoran Simic

I just found out the answer, in a controller use this:

我刚刚找到答案,在控制器中使用这个:

[self.navigationItem setHidesBackButton:YES animated:YES];

And to restore it:

并恢复它:

[self.navigationItem setHidesBackButton:NO animated:YES];

--

——

[UPDATE]

[更新]

Swift 3.0:

斯威夫特 3.0:

self.navigationItem.setHidesBackButton(true, animated:true)

回答by Zoran Simic

Add this Code

添加此代码

[self.navigationItem setHidesBackButton:YES];

回答by mattv123

In addition to removing the back button (using the methods already recommended), don't forget the user can still 'pop' to the previous screen with a left-to-right swipe gesture in iOS 7 and later.

除了移除后退按钮(使用已经推荐的方法)之外,不要忘记用户仍然可以在 iOS 7 及更高版本中使用从左到右的滑动手势“弹出”到上一个屏幕。

To disable that (when appropriate), implement the following (in viewDidLoad for example):

要禁用它(在适当的情况下),请执行以下操作(例如在 viewDidLoad 中):

 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
     self.navigationController.interactivePopGestureRecognizer.enabled = NO;

回答by Matt

Just to clarify existing answers: the hidesBackButtonproperty is the right answer, but it isn't clear in many answers what selfrefers to. Basically you should set self.navigationItem.hidesBackButton = YESin the view controller that is about to get pushed (or just got pushed) onto the UINavigationController.

只是为了澄清现有的答案:hidesBackButton财产是正确的答案,但在许多答案中并不清楚self指的是什么。基本上,您应该self.navigationItem.hidesBackButton = YES在即将被推送(或刚刚被推送)到UINavigationController.

In other words, say I have a UINavigationControllernamed myNavController. I want to put a new view on it, and when I do I don't want the back button to show anymore. I could do something like:

换句话说,假设我有一个UINavigationController命名的myNavController. 我想在它上面放一个新视图,当我这样做时,我不想再显示后退按钮。我可以做这样的事情:

UIViewController *newVC = [[UIViewController alloc] init];
//presumably would do some stuff here to set up the new view controller
newVC.navigationItem.hidesBackButton = YES;
[myNavController pushViewController:newVC animated:YES];

When the code finishes, the view controlled by newVCshould now be showing, and no back button should be visible.

代码完成后,newVC现在应该显示由 控制的视图,并且应该看不到后退按钮。

回答by Sandip Patel - SM

For hiding and showing the Back button conditionally you can use following code:

要有条件地隐藏和显示后退按钮,您可以使用以下代码:

-(void)viewDidAppear:(BOOL)animated
{
    if ([tempAry count]==0)
    {
        [self.navigationItem setHidesBackButton:YES animated:YES];
    }
    else
    {
        [self.navigationItem setHidesBackButton:NO animated:YES];
    }
    [super viewDidAppear:animated];
} 

Note: in some cases, you have to put it in viewDidAppear method instead of viewWillAppear such cases like: when you are updating array of next class into previous class and then checking condition into next class as above.

注意:在某些情况下,您必须将它放在 viewDidAppear 方法中而不是 viewWillAppear 中,例如:当您将下一个类的数组更新到上一个类中,然后将条件检查到下一个类时,如上所述。

回答by Vinod Joshi

Swift iOS (I have used following)

Swift iOS(我使用过以下)

// hide back button
        self.navigationItem.setHidesBackButton(true, animated: false)

// pgrm mark ----- ------

    // hide the back button for this view controller

    override func setEditing(editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)

        self.navigationItem.setHidesBackButton(editing, animated: animated)

    }// end setEditing

回答by M.Othman

sethidesbackbutton did not work for me for some reason

sethidesbackbutton 由于某种原因对我不起作用

I used this way ->

我用这种方式->

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 30)]] ;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 30)]] ;

回答by Muhammed Irfan

Always use Apple Documentation for simple issues they are more straightforward and lightweight :)

对于简单的问题,始终使用 Apple 文档,它们更直接、更轻量:)

Here is the syntax for Swift 3.0:

这是 Swift 3.0 的语法:

self.navigationItem.setHidesBackButton(true, animated:true)

Reference

参考

https://developer.apple.com/reference/uikit/uinavigationitem#//apple_ref/occ/instm/UINavigationItem/setHidesBackButton:animated:

https://developer.apple.com/reference/uikit/uinavigationitem#//apple_ref/occ/instm/UINavigationItem/setHidesBackButton:animated:

回答by micromanc3r

In my case I had few issues with current answers:

就我而言,目前的答案几乎没有问题:

  • inside viewDidLoad/viewWillAppear only back icon was hidden and the string "Back" was inactive but still visible
  • inside viewDidAppear the back button disappeared...but I did not want the user to see it at all
  • 在 viewDidLoad/viewWillAppear 中,只有后退图标被隐藏,字符串“后退”处于非活动状态但仍然可见
  • 在 viewDidAppear 中,后退按钮消失了......但我根本不希望用户看到它

So the solution that finally have worked for me is:

所以最终对我有用的解决方案是:

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];

    if (self) {
        [self.navigationItem setHidesBackButton:YES animated:NO];
    }

    return self;
}

回答by PowerAktar

The solution suggest by Zoran Simic didn't work for me for some reason.

由于某种原因,Zoran Simic 建议的解决方案对我不起作用。

This code did work however:

但是,此代码确实有效:

MyController* controller   =   [[MyController alloc]  init];
NSArray* array             =   [[[NSArray alloc] initWithObjects:controller, nil] autorelease];

[self.navigationController setViewControllers:array animated:NO];

[controller release];

Obviously you'd have to manipulate an NSArray to your taste to make it work for you. Hope that helps somebody :)

显然,您必须根据自己的喜好操作 NSArray 才能使其适合您。希望对某人有所帮助:)