Search code examples
iosuiappearance

Programmatically created UIButtonBarItem not picking up UIAppearance


I've added UIAppearance code in my appdelegate to customize UIButtonBarItem. The back buttons in a nav controller pick up the UIappearance. But the UIButtonBarItems I create programmically to appear on the first navbar dont get the uiappearance. Isn't UIAppearance supposed to apply to all UIButtonBarItems that are created? The same thing happens if I put in a UIButtonBarItem into a navbar in storyboard. The navbar picks up the UIAppearance but the UIButtonBarItem does not.

in appdelegate

-(void) configureAppearance
{
    // navbar background
    UIView* bk0 = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 480.0f, 44.0f)];
    UIView* bk1 = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 480.0f, 22.0f)];
    UIView* bk2 = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 22.0f, 480.0f, 22.0f)];
    [bk0 addSubview:bk1];
    [bk0 addSubview:bk2];
    bk1.backgroundColor=LIGHT_PURPLE;
    bk2.backgroundColor=DARK_PURPLE;
    UIImage* navimg=ChangeViewToImage(bk0);
    [[UINavigationBar appearance] setBackgroundImage:navimg forBarMetrics:UIBarMetricsDefault];

#define BUTTON_SIZE 34.0f
    UIView* bbk0 = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, BUTTON_SIZE, BUTTON_SIZE)];
    UIView* bbk1 = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, BUTTON_SIZE, BUTTON_SIZE)];
    bbk0.backgroundColor=[UIColor clearColor];
    bbk1.backgroundColor=DARK_PURPLE;

    [bbk0 addSubview:bbk1];
    addBorderAndShadow(bbk1, VERY_DARK_PURPLE, 1.0, 2.0);
    UIImage* bimg=ChangeViewToImage(bbk0);

    bimg = [bimg resizableImageWithCapInsets:UIEdgeInsetsMake(0.0f, BUTTON_SIZE, 0, 0)]; // this will keep it square

    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:bimg
                                                      forState:UIControlStateNormal
                                                    barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, bimg.size.height*2) forBarMetrics:UIBarMetricsDefault]; // to not show text on button but still get nav ani

    //[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setBackButtonBackgroundImage:bimg forState:UIControlStateNormal  barMetrics:UIBarMetricsDefault];

}

in one of my viewcontrollers (which doesnt get uiappearance

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;


    //AppDelegate* appdel = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    //[appdel configureAppearance];

    UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self
                                                                  action:@selector(revealMenu:)];
    self.navigationItem.leftBarButtonItem = backButton;

}

Solution

  • Try like this:

    [UIBarButtonItem apperanceWhenContainedIn: [UINavigationController class],nil] setBackgroundImage:bimg forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];