首页 文章

以编程方式显示uitabview中的隐藏选项卡

提问于
浏览
0

如何以编程方式隐藏UITabView应用程序中的某些选项卡?

如何修改 didFinishLaunchingWithOptions 以隐藏某些选项卡,例如

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

// Override point for customization after app launch.

// Add the tab bar controller's current view as a subview of the window
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];

return YES;
}

我试着补充一下

for (UIViewController *v in tabBarController.viewControllers  )
{
    UIViewController *vc = v;

    if ([vc isKindOfClass:[FirstViewController class]])
    {
         FirstViewController *myViewController = vc;
         vc.view.hidden = YES;
    }
}

但它会删除此视图的内容,并且仍会显示名为first的选项卡 . 如何删除它?

最好的祝福 .

3 回答

  • 0

    使用UIView的 hidden 属性 . 因为每个视图都是从 UIView 继承的,

  • 0

    严格来说,你不应该做这样的事情 . 标签栏的要点是显示应用程序的绝望部分,而不是上下文敏感部分 . 最好使用UIToolBar来做这类事情 . 尝试根据上下文隐藏各个标签可能会让Apple拒绝您的应用 .

    也就是说,如果你必须这样做,那么你需要使用 UITabBarControllersetViewControllers:animated: 方法 . 您无需对 FirstViewController 本身做任何事情 .

    就像是:

    NSMutableArray* controllers = [myTabBarController.viewControllers mutableCopy];
    [controllers removeObjectsInRange: NSMakeRange(0, 1)];
    [myTabBarController setViewControllers: controllers animated: YES];
    
  • 0

    我同意“Daniel T.”标签必须留在原位 .

    无论如何,我必须修改基于Web服务的选项卡,如果没有记录,则隐藏“配置文件”选项卡 .

    作为一个起点:

    1)自定义TabBarController创建自定义类

    2)实现你的逻辑 . (在示例代码中,我甚至根据网页设置更改图标...)

    /  Created by ing.conti on 6/5/18.
    //  Copyright © 2018 ing.conti. All rights reserved.
    //
    
    import UIKit
    
    class CustomTabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // by hand:
        setupTabBar()
    }
    
    final private func setImage(named: String, to: UITabBarItem){
        if let image = UIImage(named: named){
        to.selectedImage = image
        to.image = image
        }
    }
    
    final func setupTabBar(){
        guard var tbis : [UITabBarItem] = self.tabBar.items else{
            return
        }
    
        // set images, anyway.. so it's ready for future
        tbis[0].title = localized("TITLE1")
        self.setImage(named: "img1", to:tbis[0])
    
        tbis[1].title = localized("TITLE2")
        self.setImage(named: "title2", to:tbis[1])
    
        tbis[2].title = localized("PROFILE")
        self.setImage(named: "profile", to:tbis[2])
    
        // for now simply remove controller...
    
        if !LoginManager.shared.logginAllowsProfile(){
            self.viewControllers?.removeLast()
        }
    
        // as a convenience, select always 2nd:
    //  self.selectedViewController = self.viewControllers?[1]
    
    }
    

    }

相关问题