Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

When set self.navigationItem.tilte 's color .your way can't change the title color. #78

Closed
13098806890 opened this issue Jan 10, 2017 · 4 comments

Comments

@13098806890
Copy link

The only way to set self.navgitaionItem.title's color is to set TitleTextAttributes instead of the color.
for example.
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor],NSForegroundColorAttributeName,
nil];
[self.navigationController.navigationBar setTitleTextAttributes:dic];
self.navigationItem.title = @"theTitleIWantToChangeColor";

but the way you offer can;t solve this.

So, I overwrite the method - (void)night_updateColor in UINavigationBar+Night.m and add a property to support that, but this is just a temp way to solve this. Hope you offer a better way to solve this.

  • (DKColorPicker)dk_titleTintColorPicker {
    return objc_getAssociatedObject(self, @selector(dk_barTintColorPicker));
    }

  • (void)dk_setTitleTintColorPicker:(DKColorPicker)picker{
    objc_setAssociatedObject(self, @selector(dk_titleTintColorPicker), picker, OBJC_ASSOCIATION_COPY_NONATOMIC);
    UIColor *color = picker(self.dk_manager.themeVersion);
    NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
    [color copy],NSForegroundColorAttributeName,
    nil];
    [self setTitleTextAttributes:navbarTitleTextAttributes];
    [self.pickers setValue:[picker copy] forKey:@"setTitleTextAttributes:"];
    }

  • (void)night_updateColor {
    [self.pickers enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull selector, DKColorPicker _Nonnull picker, BOOL * _Nonnull stop) {
    SEL sel = NSSelectorFromString(selector);
    id result = ((DKColorPicker)picker)(self.dk_manager.themeVersion);
    if([selector isEqualToString:@"setTitleTextAttributes:"])
    {
    result = [NSDictionary dictionaryWithObjectsAndKeys:
    [result copy],NSForegroundColorAttributeName,
    nil];
    }
    [UIView animateWithDuration:DKNightVersionAnimationDuration
    animations:^{
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    [self performSelector:sel withObject:result];
    #pragma clang diagnostic pop
    }];
    }];
    }

@gbfansheng
Copy link

same question

@draveness
Copy link
Owner

这种方法确实可能会有问题,不过你可以在一个 block 中配置颜色,DKNightVersion 会在每次改变主题时,调用 block

@13098806890
Copy link
Author

13098806890 commented Mar 12, 2017 via email

@ieliwb
Copy link

ieliwb commented Jan 26, 2019

楼主解决了吗?
怎么不重写titleVIew改变nav title颜色呀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants