无法在选项卡栏中设置 BadgeValue Objective C
Failed to setBadgeValue in tab bar Objective C
我想在收到新警报时在标签栏中设置徽章值。
我曾尝试将此代码 [[self.tabBarController.tabBar.items objectAtIndex:3] setBadgeValue:@"2"];
放入 viewDidLoad
但没有成功。
这是我在 TabbarController 中的代码。
谁能帮忙查一下?非常感谢您的帮助
@interface TabbarController ()<UITabBarControllerDelegate>
@end
@implementation TabbarController
+ (void)initialize{
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = PFR11Font;
attrs[NSForegroundColorAttributeName] = ThemeBlueColor;
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];
selectedAttrs[NSForegroundColorAttributeName] = ThemeBlueColor;
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:attrs forState:UIControlStateNormal];
[item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
[UITabBar appearance].translucent = NO;
}
- (void)viewDidLoad {
[super viewDidLoad];
[[self.tabBarController.tabBar.items objectAtIndex:3] setBadgeValue:@"2"];
self.view.backgroundColor = [UIColor whiteColor];
self.delegate = self;
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
ThemeBlueColor, NSForegroundColorAttributeName,
nil] forState:UIControlStateSelected];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1.0f], NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
[self setupChildVc:[[DCHandPickViewController alloc] init] title:@"Home" image:@"tabr_01_up" selectedImage:@"tabr_01_down"];
[self setupChildVc:[[DCCommodityViewController alloc] init] title:@"Category" image:@"tabr_02_up" selectedImage:@"tabr_02_down"];
[self setupChildVc:[[Map_ViewController alloc] init] title:@"Map" image:@"tabr_05_up" selectedImage:@"tabr_05_down"];
[self setupChildVc:[[DCMyTrolleyViewController alloc] init] title:@"Cart" image:@"tabr_04_up" selectedImage:@"tabr_04_down"];
[self setupChildVc:[[Chat_ViewController alloc] init] title:@"Chat" image:@"tabr_03_up" selectedImage:@"tabr_03_down"];
}
- (void)setupChildVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
vc.navigationItem.title = title;
vc.tabBarItem.title = title;
vc.tabBarItem.image = [UIImage imageNamed:image];
vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
NavigationController *nav = [[NavigationController alloc] initWithRootViewController:vc];
[self addChildViewController:nav];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (!iPhoneX) return;
for (UIView *view in self.tabBar.subviews) {
if ([NSStringFromClass([view class]) isEqualToString:@"UITabBarButton"]) {
CGRect frame = view.frame;
frame.size.height = 48;
view.frame = frame;
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:48];
heightConstraint.priority = UILayoutPriorityDefaultHigh;
}
}
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
}
#pragma mark - <UITabBarControllerDelegate>
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
[self tabBarButtonClick:[self getTabBarButton]];
}
- (UIControl *)getTabBarButton{
NSMutableArray *tabBarButtons = [[NSMutableArray alloc]initWithCapacity:0];
for (UIView *tabBarButton in self.tabBar.subviews) {
if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]){
[tabBarButtons addObject:tabBarButton];
}
}
UIControl *tabBarButton = [tabBarButtons objectAtIndex:self.selectedIndex];
return tabBarButton;
}
- (void)tabBarButtonClick:(UIControl *)tabBarButton
{
for (UIView *imageView in tabBarButton.subviews) {
if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"transform.scale";
animation.values = @[@1.0,@1.1,@0.9,@1.0];
animation.duration = 0.3;
animation.calculationMode = kCAAnimationCubic;
[imageView.layer addAnimation:animation forKey:nil];
}
}
}
@end
有什么想法吗?
I have tried to put this code [[self.tabBarController.tabBar.items objectAtIndex:3] setBadgeValue:@"2"];
in viewDidLoad
如果该代码 在 选项卡栏控制器中,则 self.tabBarController
是 nil
,并且该代码不执行任何操作。您应该像其他地方一样说 self.tabBar
。
我想在收到新警报时在标签栏中设置徽章值。
我曾尝试将此代码 [[self.tabBarController.tabBar.items objectAtIndex:3] setBadgeValue:@"2"];
放入 viewDidLoad
但没有成功。
这是我在 TabbarController 中的代码。
谁能帮忙查一下?非常感谢您的帮助
@interface TabbarController ()<UITabBarControllerDelegate>
@end
@implementation TabbarController
+ (void)initialize{
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = PFR11Font;
attrs[NSForegroundColorAttributeName] = ThemeBlueColor;
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];
selectedAttrs[NSForegroundColorAttributeName] = ThemeBlueColor;
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:attrs forState:UIControlStateNormal];
[item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
[UITabBar appearance].translucent = NO;
}
- (void)viewDidLoad {
[super viewDidLoad];
[[self.tabBarController.tabBar.items objectAtIndex:3] setBadgeValue:@"2"];
self.view.backgroundColor = [UIColor whiteColor];
self.delegate = self;
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
ThemeBlueColor, NSForegroundColorAttributeName,
nil] forState:UIControlStateSelected];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1.0f], NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
[self setupChildVc:[[DCHandPickViewController alloc] init] title:@"Home" image:@"tabr_01_up" selectedImage:@"tabr_01_down"];
[self setupChildVc:[[DCCommodityViewController alloc] init] title:@"Category" image:@"tabr_02_up" selectedImage:@"tabr_02_down"];
[self setupChildVc:[[Map_ViewController alloc] init] title:@"Map" image:@"tabr_05_up" selectedImage:@"tabr_05_down"];
[self setupChildVc:[[DCMyTrolleyViewController alloc] init] title:@"Cart" image:@"tabr_04_up" selectedImage:@"tabr_04_down"];
[self setupChildVc:[[Chat_ViewController alloc] init] title:@"Chat" image:@"tabr_03_up" selectedImage:@"tabr_03_down"];
}
- (void)setupChildVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
vc.navigationItem.title = title;
vc.tabBarItem.title = title;
vc.tabBarItem.image = [UIImage imageNamed:image];
vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
NavigationController *nav = [[NavigationController alloc] initWithRootViewController:vc];
[self addChildViewController:nav];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (!iPhoneX) return;
for (UIView *view in self.tabBar.subviews) {
if ([NSStringFromClass([view class]) isEqualToString:@"UITabBarButton"]) {
CGRect frame = view.frame;
frame.size.height = 48;
view.frame = frame;
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:48];
heightConstraint.priority = UILayoutPriorityDefaultHigh;
}
}
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
}
#pragma mark - <UITabBarControllerDelegate>
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
[self tabBarButtonClick:[self getTabBarButton]];
}
- (UIControl *)getTabBarButton{
NSMutableArray *tabBarButtons = [[NSMutableArray alloc]initWithCapacity:0];
for (UIView *tabBarButton in self.tabBar.subviews) {
if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]){
[tabBarButtons addObject:tabBarButton];
}
}
UIControl *tabBarButton = [tabBarButtons objectAtIndex:self.selectedIndex];
return tabBarButton;
}
- (void)tabBarButtonClick:(UIControl *)tabBarButton
{
for (UIView *imageView in tabBarButton.subviews) {
if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"transform.scale";
animation.values = @[@1.0,@1.1,@0.9,@1.0];
animation.duration = 0.3;
animation.calculationMode = kCAAnimationCubic;
[imageView.layer addAnimation:animation forKey:nil];
}
}
}
@end
有什么想法吗?
I have tried to put this code
[[self.tabBarController.tabBar.items objectAtIndex:3] setBadgeValue:@"2"];
inviewDidLoad
如果该代码 在 选项卡栏控制器中,则 self.tabBarController
是 nil
,并且该代码不执行任何操作。您应该像其他地方一样说 self.tabBar
。