Iphone 如何禁用UITabBarController的“更多”部分中显示的“编辑”按钮?
在我的应用程序中(基于选项卡栏应用程序XCode模板),我使用UITabBarController显示用户可以访问的应用程序不同部分的列表 默认情况下,当项目超过5个时,UITabBarController在选项卡栏中显示“更多”按钮。此外,它允许用户选择他希望在选项卡栏中可见的项目 目前我无法实现保存和加载选项卡栏控制器的状态,因此我想禁用“编辑”按钮 有没有办法禁用/隐藏UITabBarController的“更多”导航控制器上显示的“编辑”栏按钮 我试过:Iphone 如何禁用UITabBarController的“更多”部分中显示的“编辑”按钮?,iphone,uitabbarcontroller,Iphone,Uitabbarcontroller,在我的应用程序中(基于选项卡栏应用程序XCode模板),我使用UITabBarController显示用户可以访问的应用程序不同部分的列表 默认情况下,当项目超过5个时,UITabBarController在选项卡栏中显示“更多”按钮。此外,它允许用户选择他希望在选项卡栏中可见的项目 目前我无法实现保存和加载选项卡栏控制器的状态,因此我想禁用“编辑”按钮 有没有办法禁用/隐藏UITabBarController的“更多”导航控制器上显示的“编辑”栏按钮 我试过: tabBarController
tabBarController.moreNavigationController.navigationBar.topItem.rightBarButtonItem = nil;
及
但是它们似乎不起作用。
可定制的控件
是一个数组;将其设置为空数组以禁用所有编辑
tabBarController .customizableViewControllers = nil;
tabBarController.customizableViewControllers = [NSArray arrayWithObjects:nil];
@m4rkk&@lan terrell表示代码不起作用 我无法得到它,所以我只是完全禁用了导航栏
tabBarController.moreNavigationController.navigationBar.hidden = YES;
成为
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;
}
现在它不会出现了。要考虑的关键是编辑按钮不是在控制器创建之后出现,而是在显示视图之前,我们应该静静地坐到那一刻,然后,当控制器将显示屏幕时,我们将敲击按钮,这样它就没有机会再创建它。p> 这可以像这样实现。这不是最优雅的解决方案,但它确实有效™.
// 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;
}
我不知道iOS4的情况,但是如果你把代码放在
viewdiload
vsviewwillbeen
,这很重要
这会有用的
- (void)viewWillAppear:(BOOL)animated
{
self.customizableViewControllers = nil;
}
我能够用下面的代码来实现这一点。我创建了一个
CustomTabViewController
,然后在Interface Builder中修改了我的选项卡栏控制器的类标识以使用此自定义类。下面是它使用的代码(.h和.m文件内容)。关键是将属性设置为nil,这会导致编辑按钮不显示。详情请参阅:
如果数组为空或此属性的值为零,则选项卡栏不允许重新排列任何项
#导入
@接口CustomTabBarController:UITabBarController{
}
@结束
#导入“CustomTabBarController.h”
@CustomTabBarController的实现
-(无效)viewDidLoad
{
self.leviewcontrollers=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;
}
删除“编辑”按钮
在AppDelegate.h中
@interface TestAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate, UINavigationControllerDelegate>
@接口TestAppDelegate:NSObject
如果我错了,请纠正我。只需在生命周期方法中添加一行代码,即应用程序完成启动:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
tabBarController.customizableViewControllers=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;
}
}
因为每次在此视图堆栈上推送或弹出视图控制器时,都会调用此委托方法。当我们将其他视图推送到这个“更多”视图控制器上时,我们不想这样做。对于使用Xcode大于4.0的视图(我正在为雪豹使用Xcode 4.2): 首先检查上次更改视图数组的位置。我认为将自定义视图数组设置为nil的方法无关紧要。苹果的描述是: 重要提示:在选项卡栏界面中添加或删除视图控制器也会将可自定义视图控制器数组重置为默认值,从而允许再次自定义所有视图控制器。因此,如果您修改了ViewController属性(直接修改或通过调用SetViewController:animated:方法修改),并且仍然希望限制可自定义的视图控制器,则还必须更新CustomizeLeviewController属性中的对象数组 它对我有效,所以请尝试一下。
我在这里找到了这样的描述:在“防止自定义选项卡”一章中。如果您使用NavigationController作为第一个视图控制器,并按下其中一个按钮进入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;
}
这是一个后期添加,但我认为这是一个有益的贡献。Aleks N的回答可能会造成这样一种情况,即“更多”选项卡下的每个视图控制器都会删除右按钮项(如鲍磊所述)。我想推荐使用Bao Lei的代码,但不同之处在于实现了didShowViewController委托方法 由于他的代码现在已经存在,用户点击“更多”选项卡返回基本UIMoreViewController表可能会导致属于其他ViewController的RightBarButtonim设置为零
- (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。iPhone 6 Plus在横向模式下允许在选项卡栏上使用比纵向模式更多的按钮。不幸的是,这意味着每当设备旋转时,它都会重置CustomizedLeviewController数组,这里的答案对我来说都不管用 我已经有了自己的UITabBarController子类,覆盖自定义控件的setter和getter方法是从更多屏幕中删除编辑按钮的唯一可靠方法:
- (NSArray *)customizableViewControllers
{
return nil;
}
- (void)setCustomizableViewControllers:(NSArray*)controllers
{
//do nothing
}
唯一对我有效的解决办法
self.moreNavigationController.navigationBar.topItem?.rightBarButtonItem?.title = ""
self.moreNavigationController.navigationBar.topItem?.rightBarButtonItem?.isEnabled = false
我尝试了大多数解决方案,但遇到了一个问题,旋转设备时编辑按钮会返回。旋转将重置回第一个视图控制器,然后当我返回到更多视图控制器时,编辑按钮就在那里。最好的解决方案是成为
uitabarcontrollerdelegate
,并在“更多视图”控制器成为所选视图控制器时,将右栏按钮设置为nil。这适用于iOS 11-12<
- (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;
}
}
- (NSArray *)customizableViewControllers
{
return nil;
}
- (void)setCustomizableViewControllers:(NSArray*)controllers
{
//do nothing
}
self.moreNavigationController.navigationBar.topItem?.rightBarButtonItem?.title = ""
self.moreNavigationController.navigationBar.topItem?.rightBarButtonItem?.isEnabled = false
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
}
}
}