1

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;
Madhu
  • 253
  • 1
  • 8
  • 21
  • Take a look at the answers to this question: [Custom back button in UINavigationController](http://stackoverflow.com/questions/3506297/custom-back-button-in-uinavigationcontroller). – alxmitch Jul 22 '13 at 02:45
  • I already tried most of the answers in that link. The problem is that I don't have a custom view controller class, I am using apple's EKEventViewController to create view controller and push it. I tried creating a class inheriting EKEventViewcontroller then customizing barbutton but I couldn't get it to work. May be someone can help there too? – Madhu Jul 22 '13 at 03:02
  • For your update, please check this. http://stackoverflow.com/questions/6103748/removing-right-bar-button-item-from-navigation-item – Piyush Dubey Jul 23 '13 at 07:22

2 Answers2

0

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.

Piyush Dubey
  • 2,416
  • 1
  • 24
  • 39
  • Doesn't work. This will only change the root view controller's leftbutton not the back button of EKEventViewController that I am about to push. – Madhu Jul 23 '13 at 01:59
  • I have tried this & it worked for me. I have downloaded the sample code of "EKEventViewController" from Apple website and added a new back button with an image and it worked. – Piyush Dubey Jul 23 '13 at 05:46
  • I presume it is SimpleEKDemo, I tried what you suggested again and it got me same result. How does changing leftbarbutton of root controller change the backbarbutton of the detail view controller that is about to be pushed? – Madhu Jul 23 '13 at 06:05
  • Yes, it is SimpleEKDemo. Let me check again what I have tried & will update you asap. – Piyush Dubey Jul 23 '13 at 06:24
  • Initially, I misunderstood your question. But now, I got it and have edited my answer. Please try & revert back. – Piyush Dubey Jul 23 '13 at 06:57
  • I am sorry but my question was adding a custom button to the backbarbutton item. In your updated answer, the code changes only the title of the button not the button itself. For example, facebook's app has an arrow button instead of the default back button. That is what I am looking for – Madhu Jul 23 '13 at 19:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34032/discussion-between-piyush-and-madhu) – Piyush Dubey Jul 24 '13 at 05:42
0

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