Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c ios选项卡栏自定义图像_Objective C_Ios - Fatal编程技术网

Objective c ios选项卡栏自定义图像

Objective c ios选项卡栏自定义图像,objective-c,ios,Objective C,Ios,我在ios应用程序中工作,我需要自定义tabbar以使其像 首先,我在导航控制器中创建了5个视图控制器 然后将它们放入tabbbarcontroller 我在谷歌上搜索这个问题,找到了解决办法 [self.tabBarItem setFinishedSelectedImage:<#(UIImage *)#> withFinishedUnselectedImage:<#(UIImage *)#>] [self.tabBarItem setFinishedSelectedI

我在
ios
应用程序中工作,我需要自定义
tabbar
以使其像

首先,我在导航控制器中创建了5个视图控制器 然后将它们放入
tabbbarcontroller

我在谷歌上搜索这个问题,找到了解决办法

[self.tabBarItem setFinishedSelectedImage:<#(UIImage *)#> withFinishedUnselectedImage:<#(UIImage *)#>]
[self.tabBarItem setFinishedSelectedImage:With FinishedSelectedImage:]

但它是针对iOS 5的,我需要iOS 4和iOS 5的解决方案

AppDelegate.m文件中,添加以下代码。在这段代码中,我们创建了四个视图,并将它们添加到选项卡控制器中。这些视图现在是空的,因为我们不需要在其中包含任何内容

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

{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UITabBarController *tabController = [[UITabBarController alloc] init];  


    UIViewController *viewController1 = [[UIViewController alloc] init];  


    UIViewController *viewController2 = [[UIViewController alloc] init];  


    UIViewController *viewController3 = [[UIViewController alloc] init];    


    UIViewController *viewController4 = [[UIViewController alloc] init];  

    tabController.viewControllers = [NSArray arrayWithObjects:viewController1, 

                                     viewController2, 

                                     viewController3, 

                                     viewController4, nil];

    self.window.rootViewController = tabController;  


    [self.window makeKeyAndVisible];

    return YES;

}

您可以在AppDelegate.m文件中看到一个很好的教程,添加以下代码。在这段代码中,我们创建了四个视图,并将它们添加到选项卡控制器中。这些视图现在是空的,因为我们不需要在其中包含任何内容

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

{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UITabBarController *tabController = [[UITabBarController alloc] init];  


    UIViewController *viewController1 = [[UIViewController alloc] init];  


    UIViewController *viewController2 = [[UIViewController alloc] init];  


    UIViewController *viewController3 = [[UIViewController alloc] init];    


    UIViewController *viewController4 = [[UIViewController alloc] init];  

    tabController.viewControllers = [NSArray arrayWithObjects:viewController1, 

                                     viewController2, 

                                     viewController3, 

                                     viewController4, nil];

    self.window.rootViewController = tabController;  


    [self.window makeKeyAndVisible];

    return YES;

}

您可以看到一个很好的教程

用于在ios4中自定义选项卡栏,该教程的代码不可用,您需要为我们定制选项卡栏,您可以参考此文档

或者,您也可以像下面这样制作完整的选项卡栏图像的简单逻辑

在这里,我在appdel上做了一个图像视图,并在应用程序中完成了类似的操作

self.imgV.frame=CGRectMake(0, 431, 320, 49);
[self.tabbarcontroller.view addSubview:self.imgV];

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewController];
    NSString *deviceType = [UIDevice currentDevice].model;
    NSLog(@"Device%@",deviceType);

    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad){
      //  self.imgV.frame=CGRectMake(0, 975, 768, 49);
        //[self.tabbarcontroller.view addSubview:self.imgV];

        switch (index) {
            case 0:
                self.imgV.image=[UIImage imageNamed:@"reservation_tab~iPad.png"];
                break;
            case 1:
                self.imgV.image=[UIImage imageNamed:@"place_order_tab~iPad.png"];
                break;
            case 2:
                self.imgV.image=[UIImage imageNamed:@"location_tab~iPad.png"];
                break;
            case 3:
                self.imgV.image=[UIImage imageNamed:@"favorite_tab~iPad.png"];
                break;
            case 4:

                self.imgV.image=[UIImage imageNamed:@"signature_dishes_tab~iPad.png"];

                break;
            case 5:
                self.imgV.image=[UIImage imageNamed:@"history_tab~iPad.png"];
                break;
            case 6:

                self.imgV.image=[UIImage imageNamed:@"contact_us_tab~iPad.png"];

                break;
            default:
                break;
        }

    }
    else{



    switch (index) {
        case 0:
            self.imgV.image=[UIImage imageNamed:@"reservation_tab.png"];
            break;
        case 1:
            self.imgV.image=[UIImage imageNamed:@"place_order_tab.png"];
            break;
        case 2:
            self.imgV.image=[UIImage imageNamed:@"location_tab.png"];
            break;
        case 3:
            self.imgV.image=[UIImage imageNamed:@"favorite_tab.png"];
            break;
        case 4:

                       self.imgV.image=[UIImage imageNamed:@"gallery_tab.png"];

            break;
        default:
            break;
    }
    }
    return YES;
}

对于ios4中的自定义选项卡栏,代码不可用,您需要为我们自定义选项卡栏,您可以参考此Que

或者,您也可以像下面这样制作完整的选项卡栏图像的简单逻辑

在这里,我在appdel上做了一个图像视图,并在应用程序中完成了类似的操作

self.imgV.frame=CGRectMake(0, 431, 320, 49);
[self.tabbarcontroller.view addSubview:self.imgV];

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewController];
    NSString *deviceType = [UIDevice currentDevice].model;
    NSLog(@"Device%@",deviceType);

    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad){
      //  self.imgV.frame=CGRectMake(0, 975, 768, 49);
        //[self.tabbarcontroller.view addSubview:self.imgV];

        switch (index) {
            case 0:
                self.imgV.image=[UIImage imageNamed:@"reservation_tab~iPad.png"];
                break;
            case 1:
                self.imgV.image=[UIImage imageNamed:@"place_order_tab~iPad.png"];
                break;
            case 2:
                self.imgV.image=[UIImage imageNamed:@"location_tab~iPad.png"];
                break;
            case 3:
                self.imgV.image=[UIImage imageNamed:@"favorite_tab~iPad.png"];
                break;
            case 4:

                self.imgV.image=[UIImage imageNamed:@"signature_dishes_tab~iPad.png"];

                break;
            case 5:
                self.imgV.image=[UIImage imageNamed:@"history_tab~iPad.png"];
                break;
            case 6:

                self.imgV.image=[UIImage imageNamed:@"contact_us_tab~iPad.png"];

                break;
            default:
                break;
        }

    }
    else{



    switch (index) {
        case 0:
            self.imgV.image=[UIImage imageNamed:@"reservation_tab.png"];
            break;
        case 1:
            self.imgV.image=[UIImage imageNamed:@"place_order_tab.png"];
            break;
        case 2:
            self.imgV.image=[UIImage imageNamed:@"location_tab.png"];
            break;
        case 3:
            self.imgV.image=[UIImage imageNamed:@"favorite_tab.png"];
            break;
        case 4:

                       self.imgV.image=[UIImage imageNamed:@"gallery_tab.png"];

            break;
        default:
            break;
    }
    }
    return YES;
}
试试这个 将其粘贴到.h文件

      #import <UIKit/UIKit.h>
    @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 <UIApplicationDelegate> {

        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;
    }
#导入
@类MapViewController,MenuViewController;
@接口UITabBar(颜色扩展)
-(void)recolorItemsWithColor:(UIColor*)color shadowColor:(UIColor*)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur;
@结束
@接口UItabaritem(专用)
@属性(保留,非原子)UIImage*selectedImage;
-(无效)_updateView;
@结束
@接口SegmentedControlExampleAppDelegate:NSObject{
UIWindow*窗口;
UINavigationController*导航控制器;
NSMutableArray*面包;
NSMutableArray*类别;
NSMutableArray*集合;
NSString*数据库名;
NSString*数据库路径;
MapViewController*MapViewController;
菜单视图控制器*wvTutorial;
}
@属性(非原子,保留)IBUIWindow*window;
@属性(非原子,保留)UINavigationController*navigationController;
@属性(非原子,保留)NSMutableArray*面包;
@属性(非原子,保留)NSMutableArray*类别;
@属性(非原子,保留)NSMutableArray*集合;
@属性(非原子,保留)UITabBarController*tabBarController;
@属性(非原子,保留)菜单viewcontroller*wvTutorial;
@结束
在.m文件中
#导入“SegmentedControlExampleAppDelegate.h”
#导入“SegmentManagingViewController.h”
#导入“sqlite3.h”
#导入“AtoZHomePageViewController.h”
#导入“CategoryViewHomePage.h”
#导入“CollectionsListHomePageViewController.h”
#导入“AboutUs.h”
#导入“StoreLocatorViewController.h”
#导入“UINavigationBar+CustomImage.h”
#导入“MenuViewController.h”
@实现UITabBar(颜色扩展)
-(void)recolorItemsWithColor:(UIColor*)颜色阴影颜色:(UIColor*)阴影颜色阴影偏移:(CGSize)阴影偏移阴影模糊:(CGFloat)阴影模糊
{
CGColorRef cgColor=[color cgColor];
CGColorRef cgShadowColor=[shadowColor CGColor];
对于(uitabaritem*项目在[self items]中)
如果([项目响应选择器:@selector(selectedImage)]&&
[项目响应选择器:@selector(setSelectedImage:)]&&
[项目响应选择器:@selector(_updateView)])
{
CGRect contextRect;
contextRect.origin.x=0.0f;
contextRect.origin.y=0.0f;
contextRect.size=[[item selectedImage]size];
//检索源图像并开始图像上下文
UIImage*itemImage=[项目图像];
CGSize itemImageSize=[itemImageSize];
CG点位置;
itemImagePosition.x=ceilf((contextRect.size.width-itemImageSize.width)/2);
itemImagePosition.y=ceilf((contextRect.size.height-itemImageSize.height)/2);
UIGraphicsBeginImageContext(contextRect.size);
CGContextRef c=UIGraphicsGetCurrentContext();
//设置阴影
CGContextSetShadowWithColor(c、阴影偏移、阴影模糊、cgShadowColor);
//设置透明度层和剪辑到遮罩
CGContextBeginTransparencyLayer(c,NULL);
CGContextScaleCTM(c,1.0,-1.0);
CGContextClipToMask(c,CGRectMake(itemImagePosition.x,-itemImagePosition.y,itemImageSize.width,-itemImageSize.height),[itemImageCgImage]);
//填充并结束透明层
CGContextSetFillColorWithColor(c,cgColor);
contextRect.size.height=-contextRect.size.height;
CGContextFillRect(c,contextRect);
CGContextEndTransparencyLayer(c);
//设置所选图像和结束上下文
[item setSelectedImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsSendImageContext();
//更新视图
[项目_updateView];
}
}
@结束
@实现分段控制ExampleAppDelegate
@合成窗口、tabBarController、导航控制器、面包、类别、集合、wvTutorial;
-(布尔)