代码之家  ›  专栏  ›  技术社区  ›  Panagiotis Korros

如何禁用出现在UITabbarController的“更多”部分中的“编辑”按钮?

  •  24
  • Panagiotis Korros  · 技术社区  · 16 年前

    在我的应用程序(基于标签栏应用程序xcode模板)中,我使用uitabarcontroller显示用户可以访问的应用程序不同部分的列表。

    默认情况下,当项目超过5个时,uitabarcontroller在选项卡栏中显示“更多”按钮。此外,它还允许用户选择希望在选项卡栏中可见的项目。

    目前我无法实现保存和加载选项卡栏控制器的状态,所以我想禁用“编辑”按钮。

    是否有任何方法可以禁用/隐藏出现在uitabarcontroller的“更多”导航控制器上的“编辑”栏按钮?

    我试过:

    tabBarController.moreNavigationController.navigationBar.topItem.rightBarButtonItem = nil;
    

    tabBarController.moreNavigationController.navigationBar.topItem.rightBarButtonItem.enabled = NO;
    

    但它们似乎不起作用。

    16 回复  |  直到 6 年前
        1
  •  60
  •   BastiBen    13 年前

    成为 moreNavigationController (它是uinavigationcontroller)并添加:

    - (void)navigationController:(UINavigationController *)navigationController
            willShowViewController:(UIViewController *)viewController
            animated:(BOOL)animated {
    
        UINavigationBar *morenavbar = navigationController.navigationBar;
        UINavigationItem *morenavitem = morenavbar.topItem;
        /* We don't need Edit button in More screen. */
        morenavitem.rightBarButtonItem = nil;
    }
    

    现在不会出现了。需要考虑的关键是,编辑按钮不是在控制器创建之后出现,而是在显示视图之前出现,我们应该静坐到那一刻,然后,当控制器要显示屏幕时,我们将把按钮敲出来,这样它就没有机会再创建它了。:)

        2
  •  53
  •   Ian Terrell    16 年前

    customizableViewControllers 是一个数组;将其设置为空数组以禁用所有编辑。

    tabBarController.customizableViewControllers = [NSArray arrayWithObjects:nil];
    
        3
  •  9
  •   DD_ NiravPatel    12 年前
    tabBarController .customizableViewControllers = nil;
    
        4
  •  6
  •   Jian Yu    14 年前

    我试过了,举个例子。

    在appdelegate.m中

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
        // Override point for customization after application launch.
    
        // Add the tab bar controller's view to the window and display.
        [self.window addSubview:tabBarController.view];
        [self.window makeKeyAndVisible];
    
        //setting delegate to disable edit button in more.
        tabBarController.moreNavigationController.delegate = self;
    
        return YES;
    }
    

    删除“编辑”按钮

        - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
            UINavigationBar *morenavbar = navigationController.navigationBar;
            UINavigationItem *morenavitem = morenavbar.topItem;
            /* We don't need Edit button in More screen. */
    morenavitem.rightBarButtonItem = nil;
    }
    

    在您的AppDelegate.h中

    @interface TestAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate, UINavigationControllerDelegate>
    

    如果我错了就纠正我。

        5
  •  4
  •   Dave Clemmer manu    12 年前

    我可以用下面的代码来处理这个问题。我创造了一个 CustomTabViewController 然后在InterfaceBuilder中修改了我的选项卡栏控制器的类标识以使用这个自定义类。这是它使用的代码(.h和.m文件内容)。键将属性设置为nil,这将导致不显示“编辑”按钮。有关详细信息,请参阅: http://developer.apple.com/library/ios/documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html#//apple_ref/occ/instp/UITabBarController/customizableViewControllers 如果数组为空或此属性的值为零,则选项卡不允许重新排列任何项。

    #import <UIKit/UIKit.h>
    
    @interface CustomTabBarController : UITabBarController {
    
    }
    @end
    
    #import "CustomTabBarController.h"
    
    
    @implementation CustomTabBarController
    
    - (void)viewDidLoad
    {
        self.customizableViewControllers = nil;
        [super viewDidLoad];
    }   
    
    @end
    
        6
  •  3
  •   Logan Cautrell    15 年前

    这是可以做到的。这不是最优雅的解决方案,但它的作用是。

    // Optional UITabBarControllerDelegate method
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
        [self performSelector:@selector(removeEdit) withObject:nil afterDelay:.0001];
    }
    - (void)removeEdit
    {
        tabBarController.moreNavigationController.navigationBar.topItem.rightBarButtonItem = nil;   
    }
    
        7
  •  3
  •   Eshwar Chaitanya    13 年前

    只需在生命周期方法中添加一行代码,即应用程序确实完成了启动:

    - (void)applicationDidFinishLaunching:(UIApplication *)application
    { 
        tabBarController.customizableViewControllers=nil;
    
    }
    
        8
  •  3
  •   BenMorel Manish Pradhan    11 年前

    @m4rkk&@lan terrell该代码不起作用。

    我没能拿到它,所以我把导航栏完全关掉了。

    tabBarController.moreNavigationController.navigationBar.hidden = YES;
    
        9
  •  3
  •   kamalesh kumar yadav suraj Gupta    11 年前

    我不知道IOS4,但是如果你把代码放进去就很重要了 viewDidLoad VS viewWillAppear .

    也就是说,这是可行的。

    - (void)viewWillAppear:(BOOL)animated
    {
    self.customizableViewControllers = nil;
    }
    
        10
  •  2
  •   Lee    12 年前

    如果使用导航控制器作为第一个视图控制器,并按其中一个按钮进入uitabbarcontroller。除了添加下面的代码,

    - (void)navigationController:(UINavigationController *)navigationController
            willShowViewController:(UIViewController *)viewController
            animated:(BOOL)animated 
    {
        UINavigationBar *morenavbar = navigationController.navigationBar;
        UINavigationItem *morenavitem = morenavbar.topItem;
        /* We don't need Edit button in More screen. */
        morenavitem.rightBarButtonItem = nil;
    }
    

    您需要添加这个“if语句”,以避免第一次单击第五个viewcontrollers及更高版本时出现编辑按钮。

    if (self.selectedIndex >= 4) 
    {
        self.customizableViewControllers = nil;
    }
    
        11
  •  1
  •   rubybeginner    12 年前

    在使用xcode大于4.0的代码时(我正在使用xcode 4.2 for Snow Leopard):

    首先检查上次在哪里更改视图数组。我认为,将定制视图数组设置为nil的方法并不重要。苹果的描述是:

    重要提示:在选项卡栏界面中添加或删除视图控制器也会将可自定义视图控制器阵列重置为默认值,从而允许再次自定义所有视图控制器。因此,如果修改了viewControllers属性(直接或通过调用setviewControllers:animated:method),但仍希望限制可自定义的视图控制器,则还必须更新customizableviewControllers属性中的对象数组。

    它对我有效,所以请试试看。 我在这里找到了这个描述: link to the description on developer.apple.com 在“防止定制标签”一章。

        12
  •  1
  •   Anthony F    9 年前

    iPhone6 Plus允许横向模式下的标签栏上的按钮多于纵向模式下的按钮。不幸的是,这意味着每当设备旋转时,它都会重置自定义的leviewcontrollers数组,而这里的所有答案都不适用于我。

    我已经有了自己的uitabarcontroller子类,并且为自定义控件重写setter和getter方法是从更多屏幕删除编辑按钮的唯一可靠方法:

    - (NSArray *)customizableViewControllers
    {
        return nil;
    }
    
    - (void)setCustomizableViewControllers:(NSArray*)controllers
    {
        //do nothing
    }
    
        13
  •  1
  •   Edwin Otten    9 年前

    这是一个迟来的补充,但我认为这是一个有益的贡献。aleks n的答案可以创造一种情况 右巴比顿矿 删除“更多”选项卡下的每个视图控制器(如Bao Lei所述)。我想推荐使用包雷的代码,但不同的是,它的实现 DidshowView控制器 委托方法。

    由于他的代码现在已经存在,用户点击“更多”选项卡返回到基础 更多视图控制器 表可以引起 右巴比顿矿 属于要设置为零的其他视图控制器。

    - (void)navigationController:(UINavigationController *)navigationController
           didShowViewController:(UIViewController *)viewController
                        animated:(BOOL)animated
    {
        if (navigationController.viewControllers.count == 1)
        {
            UINavigationBar *morenavbar = navigationController.navigationBar;
            UINavigationItem *morenavitem = morenavbar.topItem;
            /* We don't need Edit button in More screen. */
            morenavitem.rightBarButtonItem = nil;
        }
    }
    

    区别很小,但我花了相当长的时间才发现这个bug。

        14
  •  1
  •   Edwin Otten    9 年前

    阿列克斯·N的回答有效,但我想修改一下

    - (void)navigationController:(UINavigationController *)navigationController
          willShowViewController:(UIViewController *)viewController
                        animated:(BOOL)animated
    {
        if (navigationController.viewControllers.count == 1)
        {
            UINavigationBar *morenavbar = navigationController.navigationBar;
            UINavigationItem *morenavitem = morenavbar.topItem;
            /* We don't need Edit button in More screen. */
            morenavitem.rightBarButtonItem = nil;
        }
    }
    

    因为每次在这个视图堆栈上推送或弹出视图控制器时都会调用这个委托方法。当我们将其他视图推到这个“更多”视图控制器上时,我们不想这样做。

        15
  •  0
  •   Alejandro Luengo    7 年前

    唯一适合我的解决方案

    self.moreNavigationController.navigationBar.topItem?.rightBarButtonItem?.title = ""
    self.moreNavigationController.navigationBar.topItem?.rightBarButtonItem?.isEnabled = false
    
        16
  •  0
  •   Chris    6 年前

    我尝试了这些解决方案中的大多数,并且遇到了一个问题,即当旋转设备时,编辑按钮将返回。旋转将重置回第一个视图控制器,然后当我返回到更多视图控制器时,编辑按钮就在那里。最好的解决办法是 UITabBarControllerDelegate 当更多的视图控制器成为选定的视图控制器时,将右栏按钮设置为零。这适用于iOS 11-12。

    final class DashboardController: UITabBarController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            delegate = self
        }
    }
    
    extension DashboardController: UITabBarControllerDelegate {
        func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
            if viewController == moreNavigationController {
                moreNavigationController.navigationBar.topItem?.rightBarButtonItem = nil
            }
        }
    }