Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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
ios选项卡栏在didSelectViewController上选择的索引集图像_Ios_Tabbar - Fatal编程技术网

ios选项卡栏在didSelectViewController上选择的索引集图像

ios选项卡栏在didSelectViewController上选择的索引集图像,ios,tabbar,Ios,Tabbar,我正在通过编程创建一个选项卡栏,选项卡栏工作正常问题是我不知道如何设置选定索引,选定选项卡栏设置蓝色,设置蓝色图像 就像如何知道这个选项卡栏项目被选中一样,使用了didSelectViewController委托方法,但我不明白如何设置图像 这是使用的方法,但我不知道如何设置图像和颜色 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController

我正在通过编程创建一个选项卡栏,选项卡栏工作正常问题是我不知道如何设置选定索引,选定选项卡栏设置蓝色,设置蓝色图像 就像如何知道这个选项卡栏项目被选中一样,使用了didSelectViewController委托方法,但我不明白如何设置图像 这是使用的方法,但我不知道如何设置图像和颜色

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
这是我的密码

-(IBAction)clicka:(id)sender
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    AViewController *viewController1 = [[AViewController alloc] initWithNibName:@"AViewController" bundle:nil];





    BViewController *viewController2 = [[BViewController alloc] initWithNibName:@"BViewController" bundle:nil];



    CViewController *viewController3 = [[CViewController alloc]initWithNibName:@"CViewController" bundle:nil];



    DViewController *viewController4 = [[DViewController alloc]initWithNibName:@"DViewController" bundle:nil];



    [self.navigationController pushViewController:viewController1 animated:YES];



    self.tabBarController = [[UITabBarController alloc] init];



    self.tabBarController.delegate=self;

    self.tabBarController.viewControllers = @[viewController1,viewController2,viewController3,viewController4];



    UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:self.tabBarController];

    UIImageView   *img = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,320,100)];

    img.image=[UIImage imageNamed:@"yellow-bg.png"];

    [self.tabBarController.tabBar addSubview:img];





    UIImageView *imghome=[[UIImageView alloc]initWithFrame:CGRectMake(30.0,5,25,25)];

    imghome.image=[UIImage imageNamed:@"splash-logo.png"];

    [img addSubview:imghome];



    UIImageView *imghome1=[[UIImageView alloc]initWithFrame:CGRectMake(100.0,5,25,25)];

    imghome1.image=[UIImage imageNamed:@"chat-icon.png"];

    [img addSubview:imghome1];



    UIImageView *imghome2=[[UIImageView alloc]initWithFrame:CGRectMake(180.0,5,25,25)];

    imghome2.image=[UIImage imageNamed:@"p-icon.png"];

    [img addSubview:imghome2];

    UIImageView *imghome3=[[UIImageView alloc]initWithFrame:CGRectMake(260.0,5,25,25)];

    imghome3.image=[UIImage imageNamed:@"addddd.png"];

    [img addSubview:imghome3];

    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];


}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    NSUInteger selectedIndex = self.tabBarController.selectedIndex;
NSLog(@"%lu",(unsigned long)selectedIndex);
}

请帮助我

您可以在UIImage.h中找到渲染模式

- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode
默认情况下,如果图像作为tabbarItem附加到选项卡栏,则重新绘制模式为UIImageRenderingModeAlwaysTemplate,因此为蓝色

在设置为tabbaritem之前,应更改图像渲染模式

UIImage * originalImage = [UIImage imageNamed:@"chat-icon.png"];
UIImage * selectedBlueImage = [originalImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

选择选项卡后,将selectedBlueImage设置为imageView。比如:imageview.highlightedImage=selectedBlueImage

我不确定我是否正确理解了您的问题,但是如果您想在每个视图控制器的每个选项卡部分中相应地设置图像,您可以从每个视图控制器类中进行设置,例如,在AViewController.m中,覆盖:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
        //self.tabBarItem.title = @"Title";
        self.tabBarItem.image = [UIImage imageNamed:@"image-name"];
    }

    return self;
}

顺便说一下,您使用UITabBarController的方式是不正确的。您应该这样使用。UITabBarItem*tabBarItem=[[UITabBarItem alloc]initWithTitle:title图像:图像选择图像:选择图像];viewController1.tabBarItem=tabBarItem;对不起,我的英语是周,是的,我想选择标签栏项目图像更改,为用户知道哪个标签栏项目被选中,此代码不工作