【问题标题】:custom background on tabbar in storyboard故事板标签栏上的自定义背景
【发布时间】:2012-07-29 22:46:40
【问题描述】:

所以我不得不承认,直到大约一天前,当我添加一个新视图时,这段代码才能完美运行,所以我现在相当沮丧。

设置: 我有一个包含标签栏的故事板应用程序。在 AppDelegate 中,我有以下用于附加 CoreData

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UINavigationController *navigationController = [[tabBarController viewControllers] objectAtIndex:0];
    GamesViewController *controller = [[navigationController viewControllers] objectAtIndex:0];
    controller.managedObjectContext = self.managedObjectContext;

    return YES;
}

同样在 AppDelegate 中,我有这个方法可以将标准选项卡背景设置为我选择的图像:

- (void)customizeInterface {    
    UIImage* tabBarBackground = [UIImage imageNamed:@"tab_background"];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];
}

所以这一切都很好直到我在标签之前添加了另一个登录视图。我也必须更改最初设置的 CoreData(从我的选项卡到我的登录/初始化视图)。下面是故事板外观的新设置。

现在,当应用程序加载时...背景图像最初会像以前一样显示,但在第一个选项卡上。一旦我单击它,它会再次切换到默认渐变颜色。如果我回到第一个/初始选项卡,背景不会重新应用,它会保持为彩色渐变。

这是修改后的 applicationDidFinishLaunching 代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //instantiate local context
    NSManagedObjectContext *context = [self managedObjectContext];
    if (!context) {
        // Handle the error.
        NSLog(@"Error: Context is null");
    }

    LoginViewController *rootViewController = [LoginViewController alloc];
    rootViewController.managedObjectContext = context;

    return YES;
}

那么我尝试做的是进入我的标签栏加载的第一个 VC(GameViewController)中的 viewDidLoad 并尝试添加它来解决问题:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"dock_background"]];
}

那没用,所以我也尝试使用我在 AppDelegate 中的相同原始代码,但也没有用:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage* tabBarBackground = [UIImage imageNamed:@"tab_background"];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];
}

所以...我有点卡住了。我必须做(或不做)如此明显的事情......那里有人有任何提示/指针吗?

非常感谢 - 画了

【问题讨论】:

  • 是否要自定义标签栏
  • 哇。现在我看到了编码的深夜效果有时可以做什么。原来答案一直摆在我面前。我的视图试图加载图像“dock_background.png”而不是 tab_background.png(这是实际的文件名)。由于该图像不存在......它默认为正常的渐变背景颜色。啊!!!感谢 Neon 提供的帮助...我会记住您的代码,以备将来的项目使用,因为它看起来很有希望。

标签: iphone ios storyboard tabbar ios5


【解决方案1】:

Delegate.h 文件

@interface AppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIImageView *imgV;
@property (strong, nonatomic) UIViewController *viewController;
@property (strong, nonatomic) UITabBarController *tabBarController;

@end

Delegate.m 文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

        self.viewController = [[DeskboardVctr alloc] initWithNibName:@"DeskboardVctr" bundle:nil];




    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.delegate=self;
    self.imgV=[[UIImageView alloc] init];
    self.imgV.frame=CGRectMake(0, 0, 1024, 49);
    [[self.tabBarController tabBar] insertSubview:self.imgV atIndex:1];
    self.tabBarController.delegate=self;
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    return YES;
}

Tabbar 委托方法

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewController];
        switch (index) {
            case 0:
                self.imgV.image=[UIImage imageNamed:@"t1.png"];
                break;
            case 1:
                self.imgV.image=[UIImage imageNamed:@"t2.png"];
                break;
            case 2:
                self.imgV.image=[UIImage imageNamed:@"t3.png"];
                break;
            case 3:
                self.imgV.image=[UIImage imageNamed:@"t4.png"];
                break;
            case 4:
                self.imgV.image=[UIImage imageNamed:@"t5.png"];
                break;
            default:
                break;
        }
    return YES;
}

希望你能帮上忙 谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-04
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多