您好,我一直在尝试自定义 UItabBarItem,我发现一些代码可以自定义 UItabbaritem,但它们不只是在为我工作。当我使用这两个代码中的任何一个时,什么都没有发生。任何帮助将不胜感激。提前谢谢
[yourTabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[NSValue valueWithUIOffset:UIOffsetMake(0,0)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Helvetica" size:18.0], UITextAttributeFont, nil]
forState:UIControlStateNormal];
我也试过这个,但这个也不起作用。
[[UITabBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], UITextAttributeTextColor,
[UIColor whiteColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Rok" size:0.0], UITextAttributeFont,
nil]
forState:UIControlStateNormal];
这是我的代码
视图控制器.h
// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
UIImage *img;
UITabBarItem *tabBarItem;
}
@end
我的 ViewController.m
// ViewController.m
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor purpleColor]];
img=[UIImage imageNamed:@"image2.jpg"];
//[[UITabBarItem appearance] setTintColor:[UIColor yellowColor]];
// [tabBarItem setTitleTextAttributes:<#(NSDictionary *)#> forState:<#(UIControlState)#>
// tabBarItem=[[UITabBarItem alloc]initWithTitle:@"View1" image:img tag:0];
[[UITabBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], UITextAttributeTextColor,
[UIColor whiteColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Rok" size:0.0], UITextAttributeFont,
nil]
forState:UIControlStateNormal];
self.tabBarItem=tabBarItem;
}
@end
还有我的 AppDelegate.h
// AppDelegate.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
#import "ViewController1.h"
#import "ViewController2.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UITabBarController *tabBarController;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *vc;
@property (strong, nonatomic) ViewController1 *vc1;
@property (strong, nonatomic) ViewController2 *vc2;
@end
AppDelegate.m
// AppDelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window,vc,vc1,vc2;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
tabBarController=[[UITabBarController alloc]init];
vc=[[ViewController alloc]init];
vc1=[[ViewController1 alloc]init];
vc2=[[ViewController2 alloc]init];
// CGRect frame = CGRectMake(0.0, 0.0, 32, 48);
// UIView *v=[[UIView alloc]initWithFrame:frame];
// v.backgroundColor=[UIColor redColor];
// tabBarController.view.backgroundColor=[UIColor redColor];
// [tabBarController adds
NSArray *controllersArray=[[NSArray alloc]initWithObjects:vc,vc1,vc2, nil];
// Override point for customization after application launch.
tabBarController.viewControllers=controllersArray;
tabBarController.selectedViewController=vc;
[self.window setRootViewController:tabBarController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
@end
我还有其他 ViewContoller 但现在我的重点是一个 viewController ,如果我可以自定义一个,那么我可以为其他人做同样的事情,以节省空间和时间。谢谢