ios tabbar customization with images

前端 未结 3 1751
说谎
说谎 2021-01-16 05:31

i\'m working in ios application i need to customize tabbar to be like this

First I created 5 viewcontrollers each one in navi

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-16 06:05

    try this paste it .h file

          #import 
        @class MapViewController,MenuViewController;
        @interface UITabBar (ColorExtensions)
        - (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur;
        @end
    
        @interface UITabBarItem (Private)
        @property(retain, nonatomic) UIImage *selectedImage;
        - (void)_updateView;
        @end
    
        @interface SegmentedControlExampleAppDelegate : NSObject  {
    
            UIWindow * window;
            UINavigationController * navigationController;
            NSMutableArray *breads;
            NSMutableArray *categorys;
            NSMutableArray *collections;
            NSString *databaseName;
            NSString *databasePath;
            MapViewController *mapViewController;
    
            MenuViewController *wvTutorial;
    
    
        }
    
        @property (nonatomic, retain) IBOutlet UIWindow * window;
        @property (nonatomic, retain) UINavigationController * navigationController;
        @property (nonatomic,retain) NSMutableArray *breads;
        @property (nonatomic,retain) NSMutableArray *categorys;
        @property (nonatomic,retain) NSMutableArray *collections;
        @property (nonatomic, retain) UITabBarController *tabBarController;
        @property (nonatomic, retain) MenuViewController *wvTutorial;
    
    
        @end
    
    In .m file
    
        #import "SegmentedControlExampleAppDelegate.h"
        #import "SegmentManagingViewController.h"
        #import "sqlite3.h"
        #import "AtoZHomePageViewController.h"
        #import "CategoryViewHomePage.h"
        #import "CollectionsListHomePageViewController.h"
        #import "AboutUs.h"
        #import "StoreLocatorViewController.h"
        #import "UINavigationBar+CustomImage.h"
        #import "MenuViewController.h"
        @implementation UITabBar (ColorExtensions)
    
        - (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur
        {
            CGColorRef cgColor = [color CGColor];
            CGColorRef cgShadowColor = [shadowColor CGColor];
            for (UITabBarItem *item in [self items])
                if ([item respondsToSelector:@selector(selectedImage)] &&
                    [item respondsToSelector:@selector(setSelectedImage:)] &&
                    [item respondsToSelector:@selector(_updateView)])
                {
                    CGRect contextRect;
                    contextRect.origin.x = 0.0f;
                    contextRect.origin.y = 0.0f;
                    contextRect.size = [[item selectedImage] size];
                    // Retrieve source image and begin image context
                    UIImage *itemImage = [item image];
                    CGSize itemImageSize = [itemImage size];
                    CGPoint itemImagePosition; 
                    itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);
                    itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) / 2);
                    UIGraphicsBeginImageContext(contextRect.size);
                    CGContextRef c = UIGraphicsGetCurrentContext();
                    // Setup shadow
                    CGContextSetShadowWithColor(c, shadowOffset, shadowBlur, cgShadowColor);
    
                    // Setup transparency layer and clip to mask
                    CGContextBeginTransparencyLayer(c, NULL);
                    CGContextScaleCTM(c, 1.0, -1.0);
                    CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [itemImage CGImage]);
    
                               // Fill and end the transparency layer
                    CGContextSetFillColorWithColor(c, cgColor);
                    contextRect.size.height = -contextRect.size.height;
                    CGContextFillRect(c, contextRect);
                    CGContextEndTransparencyLayer(c);
    
    
    
                    // Set selected image and end context
                    [item setSelectedImage:UIGraphicsGetImageFromCurrentImageContext()];
                    UIGraphicsEndImageContext();
                    // Update the view
                    [item _updateView];
    
    
    
                }
    
        }
        @end
    
        @implementation SegmentedControlExampleAppDelegate
    
        @synthesize window,tabBarController, navigationController,breads,categorys,collections,wvTutorial;
    
        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
            databaseName = @"ProductsConnect_Master.sqlite";
    
    
            NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDir = [documentPaths objectAtIndex:0];
            databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    
    
            self.tabBarController = [[UITabBarController alloc] init];
            UIViewController *viewController = [[AboutUs alloc] initWithNibName:@"AboutUs" bundle:nil];
            UIViewController *viewController2 = [[StoreLocatorViewController alloc] initWithNibName:@"StoreLocatorViewController" bundle:nil];
            //UIViewController *viewController3 = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
    
            self.wvTutorial = [[MenuViewController alloc]initWithNibName:@"MenuViewController" bundle:nil];
    
    
    
    
            SegmentManagingViewController * segmentManagingViewController = [[SegmentManagingViewController alloc] init];
            self.navigationController = [[UINavigationController alloc] initWithRootViewController:segmentManagingViewController];
    
            tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController,viewController ,viewController2,wvTutorial , nil]; 
    
    
    
            [[UITabBar appearance] 
             setTintColor: [UIColor colorWithRed:120.0f/255.0f green:69.0f/255.0f blue:50.0f/255.0f alpha:1.0f]];
            [[UITabBar appearance] setSelectedImageTintColor:[UIColor colorWithRed:255.0f/255.0f green:252.0f/255.0f blue:235.0f/255.0f alpha:1.0f]];
    
            //[[UITabBar appearance] 
    
            // setBackgroundColor: [UIColor colorWithRed:255.0f/255.0f green:252.0f/255.0f blue:235.0f/255.0f alpha:0.8f]];
    
    
            navigationController.title = NSLocalizedString(@"HomePage", @"HomePage");
            navigationController.tabBarItem.image = [UIImage imageNamed:@"logoSmall"];
    
            [[UIApplication sharedApplication] setStatusBarHidden:YES];
    
            UIImage *navImage = [UIImage imageNamed:@"logoSmall.png"];
            // self.navigationItem.setImage: navImage;
    
            [[navigationController navigationBar] performSelectorInBackground:@selector(setBackgroundImage:) withObject:navImage];
    
            // UIImage *navImage = [UIImage imageNamed:@"logoSmall.png"];
            //[[navigationController navigationBar] performSelectorInBackground:@selector(setBackgroundImage:) withObject:navImage];
    
    
    
    
            [self.window addSubview:tabBarController.view];
            [segmentManagingViewController release];
            //[window addSubview:self.navigationController.view];
            [window makeKeyAndVisible];
    
            return YES;
        }
    

    i have used this code working fine for me.

提交回复
热议问题