如何隐藏在UINavigationController的“后退”按钮?(How to hide th

2019-07-18 18:40发布

你知道如何隐藏在一个UINavigationController的“返回”按钮? 此外,如何展现它回来,但我想这是非常相似的隐藏它...

就像邮件应用程序确实在iPhone上,当你点击“编辑”,同时观看电子邮件。

Answer 1:

我刚刚找到了答案,在控制器使用这样的:

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

并恢复它:

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

-

[UPDATE]

雨燕3.0:

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


Answer 2:

添加以下代码

[self.navigationItem setHidesBackButton:YES];


Answer 3:

除了去除后退按钮(已使用推荐的方法),不与iOS的7,后来左向右滑动手势忘记用户仍可以“流行”到上一画面。

要禁用(在适当的时候),实施以下(在viewDidLoad中为例):

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


Answer 4:

对于隐藏和显示后退按钮有条件,你可以使用下面的代码:

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

注:在某些情况下,你必须把它在viewDidAppear方法,而不是像viewWillAppear中这样的情况:当你更新下一班到以前的类的数组,然后检查状况到明年类如上。



Answer 5:

只是为了澄清现有的答案:在hidesBackButton属性是正确的答案,但它是不是在很多答案清楚什么self指。 基本上你应该设置self.navigationItem.hidesBackButton = YES在视图控制器即将被推(或刚刚推)到UINavigationController

换句话说,说我有一个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];

当代码完成,通过控制的视图newVC现在应该示出,且没有后退按钮应该是可见的。



Answer 6:

斯威夫特的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


Answer 7:

sethidesbackbutton并没有为我工作由于某种原因

我用这种方式 - >

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


Answer 8:

始终使用简单的问题,苹果的文档,他们更简单,轻便:)

下面是雨燕3.0的语法:

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

参考

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



Answer 9:

在我来说,我曾与当前的回答几个问题:

  • viewDidLoad中/ viewWillAppear中只有内回图标被隐藏和字符串“后退”不活跃,但仍清晰可见
  • 里面viewDidAppear后退按钮消失了,但我不希望用户看到它在所有

从而最终为我工作的解决方案是:

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

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

    return self;
}


Answer 10:

该解决方案由卓然西米奇建议,我没有工作,出于某种原因。

此代码没有工作但是:

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

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

[controller release];

很明显,你不得不处理一个NSArray你的口味,使其为你工作。 希望可以帮助别人:)



Answer 11:

在我的UIViewController子类我有这样的方法:

-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated: animated];

    // hide back button in edit mode
    [self.navigationItem setHidesBackButton:editing animated:YES];
}


Answer 12:

这会隐藏后退按钮,并与斯威夫特的添加按钮替换它:

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

    // This hides the back button while in editing mode, which makes room for an add item button
    self.navigationItem.setHidesBackButton(editing, animated: animated)

    if editing {
        // This adds the add item button
        let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
        // Use the animated setter for the left button so that add button fades in while the back button fades out
        self.navigationItem.setLeftBarButton(addButton, animated: animated)
        self.enableBackGesture(enabled: false)
    } else {
        // This removes the add item button
        self.navigationItem.setLeftBarButton(nil, animated: animated)
        self.enableBackGesture(enabled: true)
    }
}

func enableBackGesture(enabled: Bool) {
    // In addition to removing the back button and adding the add item button while in edit mode, the user can still exit to the previous screen with a left-to-right swipe gesture in iOS 7 and later. This code disables this action while in edit mode.
    if let navigationController = self.navigationController {
        if let interactivePopGestureRecognizer = navigationController.interactivePopGestureRecognizer {
            interactivePopGestureRecognizer.isEnabled = enabled
        }
    }
}


Answer 13:

斯威夫特3。

一般情况下,你应该使用每个视图控制器API苹果作为多次描述已经在此页上,但有时你需要后退按钮的直接控制。

下面的代码隐藏后退按钮,并确保抽头碰撞检测不隐藏按钮区域发生。

let emptyView = UIView(frame: .zero)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: emptyView)


Answer 14:

这会隐藏后退按钮

let backBtn = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: navigationController, action: nil)


navigationItem.leftBarButtonItem = backBtn


文章来源: How to hide the “back” button in UINavigationController?