1

我通过实现以下方法制作了一个自定义导航栏,对于所有视图都相同 - :

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    viewController.navigationItem.rightBarButtonItem = cancelButton;
// -- Adding INFO button to Navigation bar --
UIBarButtonItem *infoButton =  [[UIBarButtonItem alloc]
                                initWithTitle:@"i"
                                style:UIBarButtonItemStyleBordered
                                target:self
                                action:@selector(showInfo)];
infoButton.tag = 10;
self.navCntrl.topViewController.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:infoButton, nil];
self.navCntrl.navigationBar.tintColor = [UIColor colorWithRed:45/255.0 green:77/255.0 blue:67/255.0 alpha:1];
//    NSLog(@"Inside implemented method");
}

UINavigationControllerDelegate

在上面的方法中,我在导航项中添加了一个右键。现在我想在特定视图中隐藏这个右键。我怎样才能做到这一点?谢谢。

4

6 回答 6

7

尝试使用这个

self.navigationItem.rightBarButtonItem = nil;
self.navigationItem.rightBarButtonItem.enabled = NO;
于 2013-05-23T13:51:12.643 回答
6

在 viewDidLoad 中,尝试

self.navigationItem.rightBarButtonItem = nil;

而在 viewWillDisappear 中,别忘了放回去。

于 2013-05-23T13:53:19.887 回答
1

用这个

self.navigationItem.rightBarButtonItem = nil;
于 2013-05-23T13:51:45.723 回答
0

只需检查 viewController 是否推送了您不想要右栏按钮的类型:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

    // Replace the YourViewController with the type of the viewcontroller 
    // you want not the have the right bar button.
    if ([viewController isKindOfClass:[YourViewController class]]) {
        return;
    }

    //    viewController.navigationItem.rightBarButtonItem = cancelButton;
    // -- Adding INFO button to Navigation bar --
    UIBarButtonItem *infoButton =  [[UIBarButtonItem alloc]
                                    initWithTitle:@"i"
                                    style:UIBarButtonItemStyleBordered
                                    target:self
                                    action:@selector(showInfo)];
    infoButton.tag = 10;
    self.navCntrl.topViewController.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:infoButton, nil];
    self.navCntrl.navigationBar.tintColor = [UIColor colorWithRed:45/255.0 green:77/255.0 blue:67/255.0 alpha:1];
    //    NSLog(@"Inside implemented method");
}
于 2013-05-23T14:04:27.247 回答
0

一个很好的方法是让你的视图控制器实现一个协议。您选择名称,但它可以类似于CustomNavigationCustomization,并且只有一个方法:

@protocol CustomNavigationCustomization

- (BOOL)shouldShowRightButton;

@end

然后,您可以将方法更改为以下内容:

- (void)navigationController:(UINavigationController *)navigationController   
      willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

    BOOL shouldShowRightButton = YES;

    if ([viewController conformsToProtocol:@protocol(CustomNavigationCustomization)) {
        UIViewController <CustomNavigationCustomization> *customizableViewController =
                       (UIViewController <CustomNavigationCustomization>)viewController;

        shouldShowRightButton = [customizableViewController shouldShowRightButton];
    }

    if (shouldShowRightButton) {

        //    viewController.navigationItem.rightBarButtonItem = cancelButton;
        // -- Adding INFO button to Navigation bar --
        UIBarButtonItem *infoButton =  [[UIBarButtonItem alloc] initWithTitle:@"i"
                                                                        style:UIBarButtonItemStyleBordered
                                                                       target:self
                                                                       action:@selector(showInfo)];
        infoButton.tag = 10;
        self.navCntrl.topViewController.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:infoButton, nil];
        self.navCntrl.navigationBar.tintColor = [UIColor colorWithRed:45/255.0 green:77/255.0 blue:67/255.0 alpha:1];
        //    NSLog(@"Inside implemented method");
    }
}

请注意,导航控制器委托中的方法非常具有防御性:它检查您的视图控制器是否符合协议,然后才调用该方法。这样,您不需要在大多数视图控制器中遵守协议,只需在您希望自定义的那些中。

于 2013-05-23T13:58:47.843 回答
0

对于那些仍在寻找答案的人,这段代码在 AppDelegate.m 中对我有用

 - (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ...
    //  Get rid of the edit button in UITabBarController's moreNavigationController
            tabBarController.customizableViewControllers = nil;
    ...
        }
于 2016-04-06T00:13:41.183 回答