我有关于自定义UITabBarItem的问题
首先,您可以download my code demo
现在,我在MyAppDelegate定制UITabbarcontroller是:
-(void)configureiPhoneTabBar
{
tabViewController = (UITabBarController *)self.window.rootViewController;
UIViewController *controller1 = [[tabViewController viewControllers] objectAtIndex:0];
[self configureTabBarItemWithImageName:@"home_ON.png" : @"home.png" andText:@"Home" forViewController:controller1];
UIViewController *controller2 = [[tabViewController viewControllers] objectAtIndex:1];
[self configureTabBarItemWithImageName:@"tvChannel_ON.png" : @"tvChannel.png" andText:@"TV" forViewController:controller2];
}
-(void)configureTabBarItemWithImageName:(NSString*)imageName1 : (NSString*)imageName2 andText:(NSString *)itemText forViewController:(UIViewController *)viewController
{
UIImage* icon1 = [UIImage imageNamed:imageName1];
UIImage* icon2 = [UIImage imageNamed:imageName2];
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:itemText image:icon1 tag:0];
[item1 setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor whiteColor] }
forState:UIControlStateNormal];
[item1 setFinishedSelectedImage:icon1 withFinishedUnselectedImage:icon2];
[viewController setTabBarItem:item1];
}
我在UINavigationController中使用UITabbarcontroller,我无法从appdelegate定制uitabaritem,如果你在UITabBarController中运行代码并检查“initial”,界面显示为true,但是当在UINavigationController中检查“initial”时非常困难这样做
答案 0 :(得分:0)
如果从登录视图控制器启动应用程序,则可以从该初始viewController配置tabBarController。您可以在prepareForSegue
中执行此操作,因为您将在segue.destinationViewController
...中找到指向tabBarController的指针...
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
[self configureiPhoneTabBar:segue.destinationViewController];
}
将tabBarViewController配置代码移动到此viewController ...
-(void)configureiPhoneTabBar:(UITabBarController*)tabViewController
{
// tabViewController = (UITabBarController *)self.window.rootViewController;
UIViewController *controller1 = [[tabViewController viewControllers] objectAtIndex:0];
//...etc...
或将configureiPhoneTabBar:
发送给您的应用代理进行配置(但实际上最好将代码保留在您的应用代理之外)。
更好的是你可以继承UITabBarViewController并将配置代码放在那里,由viewDidLoad触发:
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureiPhoneTabBar];
}
-(void)configureiPhoneTabBar
{
UIViewController *controller1 = [[self viewControllers] objectAtIndex:0];
...etc...
作为另一种选择,您可以在加载到标签栏的相应viewControllers中找到配置代码。
答案 1 :(得分:0)
移动代码以将标签栏自定义为LoginViewController(它是一个分隔到标签栏控制器的标签栏)。我将此代码添加到该文件的末尾:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"idenLogin"]) {
UITabBarController *tabViewController = segue.destinationViewController;
UIViewController *controller1 = [[tabViewController viewControllers] objectAtIndex:0];
[self configureTabBarItemWithImageName:@"home_ON.png" : @"home.png" andText:@"Home" forViewController:controller1];
UIViewController *controller2 = [[tabViewController viewControllers] objectAtIndex:1];
[self configureTabBarItemWithImageName:@"tvChannel_ON.png" : @"tvChannel.png" andText:@"TV" forViewController:controller2];
}
}
-(void)configureTabBarItemWithImageName:(NSString*)imageName1 : (NSString*)imageName2 andText:(NSString *)itemText forViewController:(UIViewController *)viewController {
UIImage* icon1 = [UIImage imageNamed:imageName1];
UIImage* icon2 = [UIImage imageNamed:imageName2];
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:itemText image:icon1 tag:0];
[item1 setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor whiteColor] }
forState:UIControlStateNormal];
[item1 setFinishedSelectedImage:icon1 withFinishedUnselectedImage:icon2];
[viewController setTabBarItem:item1];
}