Pergunta

For some reason I can't use UINavigationBar on my view and I use UIToolBar instead. I was using dummy left button with clear tintColor and title button with black tintColor to properly center the title and hide the dummy left bar button, but it's not working anymore and both the dummy left button and the title button get either the main tintColor or if I disable them, they become gray.

How can I make them ideally disabled as buttons and set the proper tintColor (clear and black)?

- (void)addToolbar
{
    CGFloat barHeight = 44.0;
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.size.width - barHeight, self.view.bounds.size.height, barHeight)];

    UIBarButtonItem *flexibleSpaceButtonItem = [[UIBarButtonItem alloc]
                                                initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                target:nil action:nil];

    // Done button on the right
    UIBarButtonItem *doneButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)];

    // dummy button on the left to have centered title
    UIBarButtonItem *dummyButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];
    dummyButtonItem.enabled = NO;
    dummyButtonItem.tintColor = [UIColor clearColor];

    // title button
    UIBarButtonItem *titleButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Title text here" style:UIBarButtonItemStylePlain target:nil action:nil];
    titleButtonItem.enabled = NO;
    titleButtonItem.tintColor = [UIColor blackColor];

    // set toolbar items
    toolbar.items = @[dummyButtonItem,flexibleSpaceButtonItem, titleButtonItem, flexibleSpaceButtonItem,doneButtonItem];

    [self.view addSubview:toolbar];
}
Foi útil?

Solução

When you specify a tint for a view, the tint is automatically propagated to all subviews in the view’s hierarchy. Because UIWindow inherits from UIView, you can specify a tint color for the entire app by setting the window’s tint property using code like this:

Under application:didFinishLaunchingWithOptions:

window.tintColor = [UIColor purpleColor];

Source - iOS Transitions . Check it out under Using Tint Color section

Outras dicas

Based on your feedback I found that by doing this in AppDelegate:

self.window.tintColor = [UIColor colorWithRed:255/255.0 green:59/255.0 blue:48/255.0 alpha:1.0];
[[UITextField appearance] setTintColor:[UIColor colorWithRed:66/255.0 green:107/255.0 blue:242/255.0 alpha:1.0]];

the second line causes my issue. I now need to find out why did I put the second line there :). Thanks for your help.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top