Can't change UINavigationBar prompt color

前端 未结 9 1506
-上瘾入骨i
-上瘾入骨i 2021-01-04 01:57

I am unable to change the prompt color on my navigation bar. I\'ve tried the code below in viewDidLoad, but nothing happens.

self.navigationCont         


        
9条回答
  •  醉梦人生
    2021-01-04 02:07

    I suggest using a custom UINavigationBar subclass and overriding layoutSubviews:

    - (void)layoutSubviews {
        [super layoutSubviews];
    
        if (self.topItem.prompt) {
            UILabel *promptLabel = [[self recursiveSubviewsOfKind:UILabel.class] selectFirstObjectUsingBlock:^BOOL(UILabel *label) {
                return [label.text isEqualToString:self.topItem.prompt];
            }];
            promptLabel.textColor = self.tintColor;
        }
    }
    

    Basically I'm enumerating all UILabels in the subview hierarchy and check if their text matches the prompt text. Then we set the textColor to the tintColor (feel free to use a custom color). That way, we don't have to hardcode the private _UINavigationBarModernPromptView class as the prompt label's superview. So the code is be a bit more future-proof.

    Converting the code to Swift and implementing the helper methods recursiveSubviewsOfKind: and selectFirstObjectUsingBlock: are left as an exercise to the reader

提交回复
热议问题