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

发布于 2024-07-19 08:22:08 字数 567 浏览 8 评论 0原文

在我的应用程序中(基于选项卡栏应用程序 XCode 模板),我使用 UITabBarController 来显示用户可以访问的应用程序不同部分的列表。

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

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

有没有办法禁用/隐藏 UITabBarController 的“更多”导航控制器上显示的“编辑”栏按钮?

我尝试过:

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

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

它们似乎不起作用。

In my application (based on the Tab bar application XCode template) I use a UITabBarController to display a list of different sections of the application that the user can access.

By default, the UITabBarController displays a 'More' button in the tab bar when there are more than 5 items. Also, it allows the user to select the items that he want to be visible in the tab bar.

Currently I can't implement saving and loading the state of the tab bar controller, so I want to disable the 'Edit' button.

Is there any way to disable/hide the 'Edit' bar button that appears on the 'More' navigation controller of UITabBarController?

I tried:

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

and

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

but they don't seem to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

北城挽邺 2024-07-26 08:22:09

成为 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;
}

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

Become a delegate of moreNavigationController (it is a UINavigationController) and add this:

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

Now it won't appear. The key thing to consider is that Edit button appears not after controller creation, but before displaying the view, and we should sit silently till that moment and then, when the controller is going to display the screen, we will knock the button out so that it won't have a chance to create it again. :)

枕花眠 2024-07-26 08:22:09

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

tabBarController.customizableViewControllers = [NSArray arrayWithObjects:nil];

customizableViewControllers is an array; set it to the empty array to disable all editing.

tabBarController.customizableViewControllers = [NSArray arrayWithObjects:nil];
那请放手 2024-07-26 08:22:09
tabBarController .customizableViewControllers = nil;
tabBarController .customizableViewControllers = nil;
屋檐 2024-07-26 08:22:09

我已经尝试过,这是一个例子。

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

如果我错了,请纠正我。

i have tried and here's a example.

In 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;
}

to remove the "Edit" Button

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

In your AppDelegate.h

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

correct me if i'm wrong.

美人如玉 2024-07-26 08:22:09

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

#import <UIKit/UIKit.h>

@interface CustomTabBarController : UITabBarController {

}
@end

#import "CustomTabBarController.h"


@implementation CustomTabBarController

- (void)viewDidLoad
{
    self.customizableViewControllers = nil;
    [super viewDidLoad];
}   

@end

I was able to get this working with the following code. I created a CustomTabViewController and then modified my Tab Bar Controller's Class Identity in Interface Builder to use this custom class. Here is the code that it uses (.h and .m file contents). The key is setting the property to nil, which causes the Edit button to not be displayed. For details see: http://developer.apple.com/library/ios/documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html#//apple_ref/occ/instp/UITabBarController/customizableViewControllers
"If the array is empty or the value of this property is nil, the tab bar does not allow any items to be rearranged."

#import <UIKit/UIKit.h>

@interface CustomTabBarController : UITabBarController {

}
@end

#import "CustomTabBarController.h"


@implementation CustomTabBarController

- (void)viewDidLoad
{
    self.customizableViewControllers = nil;
    [super viewDidLoad];
}   

@end
过潦 2024-07-26 08:22:09

这可以这样实现。 它不是最优雅的解决方案,但是 It Works™。

// 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;   
}

This can be achieved like such. It is not the most elegant solution, but It Works™.

// 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;   
}
城歌 2024-07-26 08:22:09

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

- (void)applicationDidFinishLaunching:(UIApplication *)application
{ 
    tabBarController.customizableViewControllers=nil;

}

Simply add a line of code in life cycle method i.e. application did finish launching:

- (void)applicationDidFinishLaunching:(UIApplication *)application
{ 
    tabBarController.customizableViewControllers=nil;

}
陌上青苔 2024-07-26 08:22:09

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

我无法获得它,所以我完全禁用了导航栏。

tabBarController.moreNavigationController.navigationBar.hidden = YES;

@m4rkk & @lan terrell that code does not work.

I wasn't able to get it so I just disable the navigation bar altogether.

tabBarController.moreNavigationController.navigationBar.hidden = YES;
两个我 2024-07-26 08:22:09

我不了解 iOS4,但如果你将代码放在 viewDidLoadviewWillAppear 中,这很重要。

即,这会起作用。

- (void)viewWillAppear:(BOOL)animated
{
self.customizableViewControllers = nil;
}

I don't know about iOS4, but it matters if you put the code in viewDidLoad vs viewWillAppear.

Ie., this will work.

- (void)viewWillAppear:(BOOL)animated
{
self.customizableViewControllers = nil;
}
朕就是辣么酷 2024-07-26 08:22:09

如果您使用 NavigationController 作为第一个 ViewController 并按其中一个按钮进入 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 语句”,以避免在您第一次单击第 5 个及以上的 ViewController 时出现编辑按钮。

if (self.selectedIndex >= 4) 
{
    self.customizableViewControllers = nil;
}

If you use NavigationController as your 1st ViewController and press one of the button to enter UITabBarController. Then apart from adding the code below,

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

you need to add this "if statement" to avoid the edit button shows up when you first click the 5th ViewControllers and above.

if (self.selectedIndex >= 4) 
{
    self.customizableViewControllers = nil;
}
梓梦 2024-07-26 08:22:09

对于使用 Xcode 4.0 以上的版本(我正在为 Snow Leopard 使用 Xcode 4.2):

首先检查您上次更改视图数组的位置。 我认为用哪种方法将可定制视图数组设置为 nil 并不重要。 苹果的描述说:

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

它对我有用,所以请尝试一下。
我在这里找到了这个描述: 描述链接在developer.apple.com上的“防止选项卡自定义”一章。

At the ones working with Xcode greater than 4.0 (I'm working on Xcode 4.2 for Snow Leopard):

Check at first where do you change the array of views the last time. I think it doesn't matter in which method you set your customizableView-Array to nil. Apples description says:

Important: Adding or removing view controllers in your tab bar interface also resets the array of customizable view controllers to the default value, allowing all view controllers to be customized again. Therefore, if you make modifications to the viewControllers property (either directly or by calling the setViewControllers:animated: method) and still want to limit the customizable view controllers, you must also update the array of objects in the customizableViewControllers property.

It worked for me, so please try it out.
I found this description here: link to the description on developer.apple.com at chapter "Preventing the Customization of Tabs".

懒的傷心 2024-07-26 08:22:09

iPhone 6 Plus 在横向模式下的标签栏上将允许比纵向模式下更多的按钮。 不幸的是,这意味着每当设备旋转时它都会重置可定制的ViewControllers数组,并且这里的答案都不适合我。

我已经有了自己的 UITabBarController 子类,并且重写可定制视图控制器的 setter 和 getter 方法是从“更多”屏幕中删除“编辑”按钮的唯一可靠方法:

- (NSArray *)customizableViewControllers
{
    return nil;
}

- (void)setCustomizableViewControllers:(NSArray*)controllers
{
    //do nothing
}

An iPhone 6 Plus will allow more buttons on the tab bar in landscape mode than in portrait. Unfortunately this means it resets the customizableViewControllers array whenever the device is rotated, and none of the answers here worked for me.

I already had my own UITabBarController subclass and overriding the setter and getter methods for customizableViewControllers was the only reliable way to remove the Edit button from the More screen:

- (NSArray *)customizableViewControllers
{
    return nil;
}

- (void)setCustomizableViewControllers:(NSArray*)controllers
{
    //do nothing
}
海之角 2024-07-26 08:22:09

这是一个迟到的补充,但我认为这是一个有用的贡献。 Aleks N 的答案可能会造成这样一种情况:“更多选项卡”下的每个视图控制器的 rightBarButtonItem 被删除(如鲍雷提到的)。 我想推荐使用Bao Lei的代码,但不同之处在于它是didShowViewController委托方法的实现。

由于他的代码现在已存在,用户点击“更多”选项卡返回到基本 UIMoreViewController 表可能会导致属于其他 viewController 的 rightBarButtonItem 被设置为 nil。

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

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

This is a late addition but I think it is a helpful contribution. Aleks N's answer can create a situation where the rightBarButtonItem is removed for every view controller under the "More Tab" (as Bao Lei mentioned). I would like to recommend using Bao Lei's Code, but with the difference of implenting it it the didShowViewController delegate method.

As his code exists now, users tapping the "More" tab to return to the base UIMoreViewController table can cause rightBarButtonItem's belonging to other viewControllers to be set to nil.

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

The distinction is small but it took me a considerable amount of time to find this bug.

丶视觉 2024-07-26 08:22:09

Aleks 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;
    }
}

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

Aleks N's answer works, but I'd like to modify a little bit

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

Since this delegate method is called every time when a view controller is pushed or popped on this view stack. When we are pushing other views onto this "More" view controller, we don't want to do this.

苹果你个爱泡泡 2024-07-26 08:22:09

唯一对我有用的解决方案

self.moreNavigationController.navigationBar.topItem?.rightBarButtonItem?.title = ""
self.moreNavigationController.navigationBar.topItem?.rightBarButtonItem?.isEnabled = false

The only solution that worked for me

self.moreNavigationController.navigationBar.topItem?.rightBarButtonItem?.title = ""
self.moreNavigationController.navigationBar.topItem?.rightBarButtonItem?.isEnabled = false
心奴独伤 2024-07-26 08:22:09

我尝试了大部分解决方案,但遇到了旋转设备时编辑按钮会返回的问题。 旋转将重置回第一个视图控制器,然后当我返回到更多视图控制器时,编辑按钮就在那里。 最好的解决方案是成为 UITabBarControllerDelegate 并在 more 视图控制器成为所选视图控制器时将右侧栏按钮设置为 nil。 这适用于 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
        }
    }
}

I tried most of these solutions and was running into an issue where the edit button would return when rotating the device. The rotation would reset back to the first view controller, then when i returned to the more view controller, the edit button was there. The best solution was to become the UITabBarControllerDelegate and set the right bar button to nil anytime the more view controller became the selected view controller. This is working for 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
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文