我想通过将 UITabbarController 的子类添加到我的项目来创建自定义 UITabbarController。
自定义标签栏包含选中和未选中状态的自定义图标以及我要添加的标签背景。
我将代表添加到我的 .h 文件中,如下所示:
#import <UIKit/UIKit.h>
@interface CustomTabViewController : UITabBarController <UITabBarDelegate,UITabBarControllerDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
在我的 viewDidLoad 方法的实现文件中,我声明了我想要使用的所有图像。
- (void)viewDidLoad
{
[super viewDidLoad];
UITabBarController *tabController = [[UITabBarController alloc] init];
UIImage *tabBackground = [[UIImage imageNamed:@"TabBar@2x.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 320, 60)];
[[UITabBar appearance] setBackgroundImage:tabBackground];
self.window.rootViewController = tabController;
UIImage *selectedImage = [UIImage imageNamed:@"meinGuthabenLogoSelected@2x.png"];
UIImage *unSelectedImage = [UIImage imageNamed:@"meinGuthabenLogoUnselected@2x.png"];
UITabBar *tabBar = (UITabBar *)tabController.tabBar;
UITabBarItem *item1 = [tabBar.items objectAtIndex:0];
[item1 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unSelectedImage];
selectedImage = [UIImage imageNamed:@"aufladenSelected@2x.png"];
unSelectedImage = [UIImage imageNamed:@"aufladenUnselected@2x.png"];
UITabBarItem *item2 = [tabBar.items objectAtIndex:1];
[item2 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unSelectedImage];
selectedImage = [UIImage imageNamed:@"bezahlOptionenSelected@2x.png"];
unSelectedImage = [UIImage imageNamed:@"bezahlOptionenUnselected@2x.png"];
UITabBarItem *item3 = [tabBar.items objectAtIndex:2];
[item3 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unSelectedImage];
selectedImage = [UIImage imageNamed:@"meinKontoSelected@2x.png"];
unSelectedImage = [UIImage imageNamed:@"meinKontoUnselected@2x.png"];
UITabBarItem *item4 = [tabBar.items objectAtIndex:3];
[item4 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unSelectedImage];
}
但是当我运行该应用程序时,默认选项卡栏控制器没有任何更改。我还将它的类设置为我创建的自定义类。
我想问一下我是否在这里使用了正确的方法,或者我需要实施其他方法吗?
先感谢您!
花岗岩