【问题标题】:iPhone : How to remove right button from navigation bar in a view controller?iPhone:如何从视图控制器的导航栏中删除右键?
【发布时间】:2013-05-23 13:46:43
【问题描述】:

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

- (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

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

【问题讨论】:

  • 设置 self.navigationItem.rightBarButtonItem=Nil;当你想隐藏它时。
  • 我想隐藏右键,为什么要用leftbarbutton?
  • 抱歉输入错误现在修复它。
  • 已经试过了,不行。

标签: iphone ios uinavigationcontroller uinavigationbar


【解决方案1】:

尝试使用这个

self.navigationItem.rightBarButtonItem = nil;
self.navigationItem.rightBarButtonItem.enabled = NO;

【讨论】:

    【解决方案2】:

    在viewDidLoad中,试试

    self.navigationItem.rightBarButtonItem = nil;
    

    并且在 viewWillDisappear 中,别忘了放回去。

    【讨论】:

      【解决方案3】:

      使用这个

      self.navigationItem.rightBarButtonItem = nil;
      

      【讨论】:

        【解决方案4】:

        一个很好的方法是让你的视图控制器实现一个协议。您选择名称,但它可以是 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");
            }
        }
        

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

        【讨论】:

          【解决方案5】:

          只需检查 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");
          }
          

          【讨论】:

            【解决方案6】:

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

             - (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
                ...
                //  Get rid of the edit button in UITabBarController's moreNavigationController
                        tabBarController.customizableViewControllers = nil;
                ...
                    }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-05-04
              相关资源
              最近更新 更多