开发者

show hide tab in uitabview programmatically

开发者 https://www.devze.com 2023-02-20 10:28 出处:网络
How to programmatically hide certain tab/s in UITabView application? How to modify didFinishLaunchingWithOptionsto hide certain tab , first for example

How to programmatically hide certain tab/s in UITabView application?

How to modify didFinishLaunchingWithOptions to hide certain tab , first for example

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
开发者_JAVA百科
    // 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;
}

I tried to add

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

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

but it removes the content of this view and the tab named first still appear. How to remove it too?


use hidden property of UIView . since every view is inherited from UIView,


Strictly speaking, you shouldn't do such a thing. The point of a tab bar is to present desperate parts of your app, not context sensitive parts. Better to use a UIToolBar for that sort of thing. Trying to hide individual tabs depending on context may get your app rejected by Apple.

That said, if you must do it then you need to use UITabBarController's setViewControllers:animated: method. You don't have to do anything to your FirstViewController itself.

Something like:

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


I agree with "Daniel T." Tabs must stay in place.

Anyway I had to modify tabs based on web services, hiding "profile" tab if not logged.

As a starting point:

1) customise TabBarController creating a custom class

2) implement your logic. (in sample code I even change icons... based on web settings..)

/  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]

}

}

0

精彩评论

暂无评论...
验证码 换一张
取 消