Question

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;
Was it helpful?

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];

OTHER TIPS

When you push the event view controller, the Back button will appear with the title "Back" by default, so you do not have to change it manually. However, if you decide to change the Back button, you can do so by assigning a new object of type UIBarButtonItem to the backBarButtonItem property of your navigation item. For example, you can modify the pushController: method to give our root view controller a custom Back button before pushing the event view controller.

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.eventStore = [[EKEventStore alloc]init];

    NSTimeInterval nsYear = 1 * 365 * 24.0f *60.0f * 60.0f;
    NSDate *startDate = [[NSDate date] dateByAddingTimeInterval:-nsYear];
    NSDate *endDate = [NSDate date];

    NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:self.eventStore.calendars];

    NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];

    if ([events count]> 0)
    {
        EKEvent *event = [events objectAtIndex:0];
        EKEventViewController *controller = [[EKEventViewController alloc]init];
        controller.event = event;
        controller.allowsEditing = YES;
        controller.allowsCalendarPreview = YES;
        controller.delegate = self;

        self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Go BACK OK" style:UIBarButtonItemStylePlain target:nil action:nil];
        [self.navigationController pushViewController:controller animated:YES];

    }

}

Hope, this will help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top