[英]ios Tabbar selected index set image on didSelectViewController
我正在通過編程方式創建一個選項卡欄,選項卡欄正常工作問題是我不知道如何設置所選索引,所選選項卡欄設置為藍色,設置藍色圖像selected,使用了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
我不確定我是否正確理解了您的問題,但是如果要在每個View Controller的每個TabBar部分中分別設置圖像,則可以從每個View Controller類(例如在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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.