自定义UITabBarController使用子类

时间:2013-10-30 09:30:16

标签: xcode uitabbarcontroller subclass

我想通过向我的项目添加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];

}

但是当我运行应用程序时,默认的标签栏控制器没有任何变化。我还将其'class'设置为我创建的自定义类。

我想问一下我是否在这里使用正确的方法,还是有其他方法需要实现?

提前谢谢!

花岗岩

1 个答案:

答案 0 :(得分:1)

您还需要为tabBar设置框架,如下所示:

CGRect smallFrame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width-300, frame.size.height-100);

tabController = [[UITabBarController alloc] init];

tabController.view.frame = smallFrame;