我在tabbar中添加图片。 iamge大小为49px。添加工作正常。但布局有问题。所有tabbar项目图片的顶部都在tabbar的框架之外,并且有ablank空间向下。
如何解决这个问题?
这就是我创建Tabbar的方法
localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:3];
myhomeVC = [[EventListView alloc] initWithNibName:@"EventListView_iPhone" bundle:nil];
homeNavBar=[[UINavigationController alloc]initWithRootViewController:myhomeVC];
homeNavBar.tabBarItem.title=nil;
groupVC = [[ItineryView alloc] initWithNibName:@"ItineryView_iPhone" bundle:nil];
groupNavBar=[[UINavigationController alloc]initWithRootViewController:groupVC];
//groupNavBar.tabBarItem.title=@"";
uploadVC = [[FilesView alloc] initWithNibName:@"FilesView_iPhone" bundle:nil];
uploadNavBar=[[UINavigationController alloc]initWithRootViewController:uploadVC];
//uploadNavBar.tabBarItem.title=@"";
searchVC = [[PhotosView alloc] initWithNibName:@"PhotosView_iPhone" bundle:nil];
searchNavBar=[[UINavigationController alloc]initWithRootViewController:searchVC];
//searchNavBar.tabBarItem.title=@"";
nearbyVC = [[AttendeesView alloc] initWithNibName:@"AttendeesView_iPhone" bundle:nil];
nearbyNavBar=[[UINavigationController alloc]initWithRootViewController:nearbyVC];
//nearbyNavBar.tabBarItem.title=@"";
[localViewControllersArray addObject:homeNavBar];
[localViewControllersArray addObject:groupNavBar];
[localViewControllersArray addObject:uploadNavBar];
[localViewControllersArray addObject:searchNavBar];
[localViewControllersArray addObject:nearbyNavBar];
appDelegate.tabBarController.viewControllers = localViewControllersArray;
[self.parentViewController.view setHidden:YES];
appDelegate.window.rootViewController = appDelegate.tabBarController;
并设置像这样的tabbar项目 -
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"navHomeHL.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"navHome.png"]];
} }
return self;
}
答案 0 :(得分:0)
试试这个......几天前我得到同样的问题,这段代码修好了。
UIViewController *viewController1, *viewController2,*viewController3;
viewController1 = [[ViewController alloc] init];
viewController2 = [[FormStatusViewController alloc] initWithNibName:@"FormStatusViewController" bundle:nil];
viewController3 = [[DocumentsViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *nav1 =[[UINavigationController alloc] initWithRootViewController:viewController2];
UINavigationController *nav2 =[[UINavigationController alloc] initWithRootViewController:viewController3];
nav.navigationBarHidden=YES;
nav1.navigationBarHidden=YES;
nav2.navigationBarHidden=YES;
NSArray *viewsArray = [[NSArray alloc] initWithObjects:nav,nav1,nav2, nil];
self.formTabBar= [[UITabBarController alloc] init];
[self.formTabBar setViewControllers:viewsArray];
UITabBar *tabBar = self.formTabBar.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
tabBarItem1.title = @"FORM";
tabBarItem2.title = @"STATUS";
tabBarItem3.title = @"DOCUMENTS";
UIImage *tabBackground = [[UIImage imageNamed:@"tab_bg.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UITabBar appearance] setBackgroundImage:tabBackground];
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"form.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"form_h.png"]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"status.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"status_h.png"]];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"documents.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"documents_h.png"]];
} else {
tabBarItem1.selectedImage = [[UIImage imageNamed:@"form_h.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.image = [[UIImage imageNamed:@"form.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.selectedImage = [[UIImage imageNamed:@"status_h.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.image = [[UIImage imageNamed:@"status.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.selectedImage = [[UIImage imageNamed:@"documents_h.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.image = [[UIImage imageNamed:@"documents.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
}
[[UITabBar appearance] setSelectionIndicatorImage:
[UIImage imageNamed:@"tab_select_indicator.png"]];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateHighlighted];
希望这会对你有所帮助