如何禁用出现在UITabBarController的更多部分中的编辑按钮?
-
05-07-2019 - |
题
在我的应用程序中(基于Tab栏应用程序XCode模板)我使用UITabBarController来显示用户可以访问的应用程序的不同部分的列表。
默认情况下,当有超过5个项目时,UITabBarController会在标签栏中显示“更多”按钮。此外,它允许用户选择他希望在标签栏中显示的项目。
目前我无法实现保存和加载标签栏控制器的状态,所以我想禁用“编辑”按钮。
有没有办法禁用/隐藏UITabBarController的“更多”导航控制器上显示的“编辑”栏按钮?
我试过了:
tabBarController.moreNavigationController.navigationBar.topItem.rightBarButtonItem = nil;
和
tabBarController.moreNavigationController.navigationBar.topItem.rightBarButtonItem.enabled = NO;
但它们似乎不起作用。
解决方案
成为 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;
}
现在它不会出现。要考虑的关键是编辑按钮不是在控制器创建之后,而是在显示视图之前出现,我们应该静静地坐到那一刻,然后,当控制器要显示屏幕时,我们将按下按钮以便它没有机会再创造它。 :)
其他提示
customizableViewControllers
是一个数组;将其设置为空数组以禁用所有编辑。
tabBarController.customizableViewControllers = [NSArray arrayWithObjects:nil];
tabBarController .customizableViewControllers = nil;
我试过,这是一个例子。
在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>
如果我错了,请纠正我。 我能够使用以下代码实现此目的。我创建了一个 CustomTabViewController
,然后在Interface Builder中修改了我的Tab Bar Controller的Class Identity来使用这个自定义类。这是它使用的代码(.h和.m文件内容)。关键是将属性设置为nil,这会导致不显示“编辑”按钮。有关详细信息,请参阅: http://developer.apple.com/library/ios/documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html#//apple_ref/occ/instp/UITabBarController/customizableViewControllers
“如果数组为空或此属性的值为nil,则标签栏不允许重新排列任何项目。”
#import <UIKit/UIKit.h>
@interface CustomTabBarController : UITabBarController {
}
@end
#import "CustomTabBarController.h"
@implementation CustomTabBarController
- (void)viewDidLoad
{
self.customizableViewControllers = nil;
[super viewDidLoad];
}
@end
这可以这样实现。它不是最优雅的解决方案,但它可以工作&#8482;。
// 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;
}
只需在生命周期方法中添加一行代码即应用程序完成启动:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
tabBarController.customizableViewControllers=nil;
}
@ m4rkk&amp; @lan terrell代码不起作用。
我无法得到它所以我只是完全禁用了导航栏。
tabBarController.moreNavigationController.navigationBar.hidden = YES;
我不知道iOS4,但是如果你把代码放在 viewDidLoad
vs viewWillAppear
中就很重要了。
- (void)viewWillAppear:(BOOL)animated
{
self.customizableViewControllers = nil;
}
如果您使用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个ViewControllers及以上时,避免显示编辑按钮。
if (self.selectedIndex >= 4)
{
self.customizableViewControllers = nil;
}
使用Xcode大于4.0的人(我正在使用Xcode 4.2 for Snow Leopard):
首先检查上次在哪里更改视图数组。我认为将customizableView-Array设置为nil的方法无关紧要。苹果描述说:
重要说明:在标签栏界面中添加或删除视图控制器也会将可自定义视图控制器数组重置为默认值,从而允许再次自定义所有视图控制器。因此,如果您对viewControllers属性进行修改(直接或通过调用setViewControllers:animated:方法)并仍希望限制可自定义的视图控制器,则还必须更新customizableViewControllers属性中的对象数组。
它对我有用,所以请试一试。 我在这里找到了这个描述:链接到描述在developer.apple.com 的“防止自定义选项卡”一章中。
iPhone 6 Plus将允许横向模式下的标签栏上的按钮比纵向更多。不幸的是,这意味着它会在旋转设备时重置customizableViewControllers数组,并且这里没有任何答案适用于我。
我已经有了自己的UITabBarController子类,并且覆盖了customizableViewControllers的setter和getter方法是从More屏幕中删除Edit按钮的唯一可靠方法:
- (NSArray *)customizableViewControllers
{
return nil;
}
- (void)setCustomizableViewControllers:(NSArray*)controllers
{
//do nothing
}
这是一个迟到但我认为这是一个有用的贡献。 Aleks N的回答可以创建一种情况,即在“更多标签”下为每个视图控制器删除 rightBarButtonItem 。 (正如鲍磊所说)。我想推荐使用宝磊的代码,但不同的是实现它的 didShowViewController 委托方法。
由于他的代码现在存在,用户点击“更多”代码。 tab返回基础 UIMoreViewController 表可能导致属于其他viewControllers的 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;
}
}
区别很小,但我花了相当多的时间来发现这个错误。
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;
}
}
由于每次在此视图堆栈上按下或弹出视图控制器时都会调用此委托方法。当我们将其他观点推到这个“更多”时查看控制器,我们不想这样做。
唯一对我有用的解决方案
self.moreNavigationController.navigationBar.topItem?.rightBarButtonItem?.title = ""
self.moreNavigationController.navigationBar.topItem?.rightBarButtonItem?.isEnabled = false
我尝试了大部分这些解决方案,并遇到了旋转设备时编辑按钮会返回的问题。旋转将重置回第一个视图控制器,然后当我返回到更多视图控制器时,编辑按钮就在那里。最好的解决方案是成为 UITabBarControllerDelegate
,并在更多视图控制器成为选定的视图控制器的任何时候将右侧栏按钮设置为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
}
}
}