Search code examples
iphoneuiviewcontrollerekeventchildviewcontrollerbackbarbuttonitem

How to customize back barbutton on EKEventViewcontroller


It tried the following code segment to customize the back bar button with my own button. This had no effect as it looked like default back button.

    EKEventViewController*eventView = [[EKEventViewController alloc] initWithNibName:nil bundle:nil];
    eventView.event = closestEvent;
    eventView.allowsEditing = NO;
    UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:[UIImage imageNamed:@"closeButton.png"] forState:UIControlStateNormal];
    leftButton.frame = CGRectMake(0, 0, 25, 25);
    [leftButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];

    [self.navigationController pushViewController:eventView animated:YES];

I also tried to put the EKEventViewController as a child view of a another view controller which I had no clue how to get it right. Either way I'd simply like to customize the back button.

Update, I tried this:

 eventView.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];

Works but then a done button is automatically added on the right-side(may be at runtime?) I tried to nil the right bar button but had no effect:

  eventView.navigationItem.rightBarButtonItem = nil;

Solution

  • What is occurring is that when you push the EKEventViewController the object is allocated but the views are not yet loaded. The solution I found is using UIAppearance API. Try the following call.

     NSDictionary *textAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};   
     [[UIBarButtonItem appearance] setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
    

    Unfortunately this will affect all the UIBarbuttonItems in your app and the following code does not work for me. So you may have to manually set other instances of UIBarbuttonItem

    This code does not work for me

    [[UIBarButtonItem appearanceWhenContainedIn:[EKEventViewController class], nil] setTitleTextAttributes:textAttributes forState:UIControlStateNormal];