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

时间:2011-03-28 10:39:40

标签: iphone objective-c

如何以编程方式隐藏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 :(得分:0)

使用UIView的hidden属性。因为每个视图都继承自UIView

答案 1 :(得分:0)

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

也就是说,如果你必须这样做,那么你需要使用UITabBarController的{​​{1}}方法。您无需对setViewControllers:animated:本身做任何事情。

类似的东西:

FirstViewController

答案 2 :(得分:0)

我同意“丹尼尔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]

}

}