0
votes

I am using iOS 5.1 with StoryBoards. I want to change the tint of the standard page curl button, but I don't seem to be able too.

The button is on a View Controller and is a pushed View Controller of a Navigation Controller. The toolbar is inferred, as I changed the tint on the Navigation Controller. The toolbar appears in the desired color.

My viewDidLoad method is as follows:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // adjust the color of the curl button
    if (self.curlButton) {
        NSMutableArray * toolbarItems = [self.toolbarItems mutableCopy];
        [toolbarItems removeObject:self.curlButton];
        UIColor *barColor = self.navigationController.toolbar.tintColor;
        UIBarButtonItem *newPageCurl = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPageCurl target:self.curlButton.target action:self.curlButton.action];
        newPageCurl.tintColor = barColor;
        NSLog(@"%@", newPageCurl.tintColor);
        [toolbarItems addObject:newPageCurl];
        self.toolbarItems = toolbarItems;
    }
}

self.curlButton is an outlet for the page curl button that is configured in Interface Builder.

I already tried just setting the tint in self.curlButton, which is initially nil, but that didn't work, the button was the same blue tint.

Some of the answers on SO say you have to create a new button programmatically, which is what I am trying here.

I have verified that the barColor contains the correct color. I commented out the removeObject line and that correctly produced two curl buttons, but both were blue.

The NSLog verifies that the newPageCurl button has the correct tint set.

I tried doing all this in viewDidAppear instead, in case the toolbar needed to be displayed first, but the button was still blue and didn't use the new tint.

1

1 Answers

0
votes

starting in iOS 5, you should be able to use appearance selector to do this.

(edit, sorry to have led astray w.r.t. the curl button. the appearance or appearanceWhenContainedIn: appear to work for everything except the curl button … even other system bar button items. for curl, one user said that the solution at UIBarButtonSystemItem PageCurl does not change color with the toolbar worked, but when i tested it out, it didn't work for me.)

    if ([[UIBarButtonItem class] respondsToSelector:@selector(appearanceWhenContainedIn:)])
    {
        id barButtonAppearance
          = [UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil];
        [barButtonAppearance setTintColor:self.navigationController.toolbar.tintColor];
        barButtonAppearance
          = [UIBarButtonItem appearanceWhenContainedIn:[UIToolBar class], nil];
        // you'll have to have toolbar already, or pick something like [UIColor chartreuseColor] or whatever
        [barButtonAppearance setTintColor:toolbar.tintColor];
    }