UITabBar中的自定义颜色

pix*_*x0r 40 iphone objective-c

是否可以在UITabBar中使用自定义颜色和背景图像?我意识到Apple希望每个人都使用相同的蓝色和灰色标签栏,但有没有办法自定义这个?

其次,即使我创建自己的TabBar视图控制器,以及自定义图像,这是否会违反Apple的人机界面指南?

小智 30

我在Silent Mac Design上找到了答案.

我这样实现:

首先创建UITabBarContoller的子类

// CustomUITabBarController.h

#import <UIKit/UIKit.h>

@interface CustomUITabBarController: UITabBarController {
  IBOutlet UITabBar *tabBar1;
}

@property(nonatomic, retain) UITabBar *tabBar1;

@end
Run Code Online (Sandbox Code Playgroud)

 

// CustomUITabBarController.m

#import "CustomUITabBarController.h"

@implementation CustomUITabBarController

@synthesize tabBar1;

- (void)viewDidLoad {
  [super viewDidLoad];

  CGRect frame = CGRectMake(0.0, 0, self.view.bounds.size.width, 48);

  UIView *v = [[UIView alloc] initWithFrame:frame];

  [v setBackgroundColor:[[UIColor alloc] initWithRed:1.0
                                               green:0.0
                                                blue:0.0
                                               alpha:0.1]];

  [tabBar1 insertSubview:v atIndex:0];
  [v release];
}

@end
Run Code Online (Sandbox Code Playgroud)

在你的Nib文件中用CustomUITabBarController替换你的TabBar控制器的类.

  • 这对我来说有以下变化:我将UITabBar而不是UITabBarController子类化,我覆盖了`-drawRect:(CGRect)rect`而不是`-viewDidLoad`,我把我的色调UIView作为`self`的子视图. (8认同)
  • 虽然这很有效,但Apple在他们的文档中特别指出不是UITabBarController的子类.所以这需要你自担风险. (7认同)
  • 这根本没有效果.此外,Mac设计似乎已经消失了.这个答案应该检查错误... (3认同)

Dan*_*n J 20

仅供参考,从iOS 5开始,您可以自定义UITabBar的各个方面,包括使用backgroundImage属性设置其背景图像.

iOS 5中新的UITabBar "自定义外观"属性包括:

backgroundImage 
selectedImageTintColor  
selectionIndicatorImage  
tintColor  
Run Code Online (Sandbox Code Playgroud)

鉴于Apple已经在iOS 5中引入了这些方法,那么他们可能会更加同情为早期操作系统定制UITabBar.这个网站说Twitter应用程序使用自定义标签栏,所以这可能是Apple将这样的应用程序放入App Store的更多原因,但不能保证!

  • 鉴于iOS5现在非常普及,这确实是唯一合理的方法. (5认同)

Sag*_*ari 14

使用以下图像(假设,tabBar有5个标签,如下所示)

  • 在此输入图像描述
  • 在此输入图像描述
  • 在此输入图像描述
  • 在此输入图像描述
  • 在此输入图像描述

使用"TabBar应用程序"模板创建一个新项目并放置以下代码.

AppDel.h文件的内容.

#import <UIKit/UIKit.h>

@interface cTabBarAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {

}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) IBOutlet UIImageView *imgV;

@end
Run Code Online (Sandbox Code Playgroud)

AppDel.m文件的内容.

#import "cTabBarAppDelegate.h"

@implementation cTabBarAppDelegate
@synthesize window=_window;
@synthesize tabBarController=_tabBarController;
@synthesize imgV = _imgV;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.tabBarController.delegate=self;
    self.imgV.frame=CGRectMake(0, 425, 320, 55);
    [self.tabBarController.view addSubview:self.imgV];
    self.tabBarController.selectedIndex=0;
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewController];
    switch (index) {
        case 0:
            self.imgV.image=[UIImage imageNamed:@"tBar1.png"];
            break;
        case 1:
            self.imgV.image=[UIImage imageNamed:@"tBar2.png"];
            break;
        case 2:
            self.imgV.image=[UIImage imageNamed:@"tBar3.png"];
            break;
        case 3:
            self.imgV.image=[UIImage imageNamed:@"tBar4.png"];
            break;
        case 4:
            self.imgV.image=[UIImage imageNamed:@"tBar5.png"];
            break;
        default:
            break;
    }
    return YES;
}
Run Code Online (Sandbox Code Playgroud)


Ric*_*hen 9

在***ViewController.m的开头添加以下内容可能有助于设置UITabBar的背景图像.


@implementation UITabBar (CustomImage)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed: @"background.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
Run Code Online (Sandbox Code Playgroud)


Jen*_*sen 6

如果您想为图标(而不仅仅是背景)使用自定义颜色而不是默认的灰色和蓝色,请执行以下操作:http://blog.theanalogguy.be/2010/10/06/custom-colored- uitabbar-图标/

基本上,您需要为每个选定的选项卡创建完整的标签栏图像(背景和图标和文本),并将您的UITabBarItem设置为无图标和无标题,并将图像作为ViewWillAppear中的UIImageView插入标签栏:

Apple也不介意,因为我们没有使用任何私有API.