如何在EKEventViewcontroller上自定义后退按钮

时间:2013-07-22 02:35:50

标签: iphone uiviewcontroller ekevent childviewcontroller backbarbuttonitem

尝试使用以下代码段自定义后栏按钮。这没有效果,因为它看起来像默认后退按钮。

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

我还尝试将EKEventViewController作为另一个视图控制器的子视图,我不知道如何正确使用它。无论哪种方式,我只想自定义后退按钮。

更新,我试过了:

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

工作但是然后在右侧自动添加一个完成按钮(可能在运行时?)我试图使右边的条形按钮无效但是没有效果:

  eventView.navigationItem.rightBarButtonItem = nil;

2 个答案:

答案 0 :(得分:0)

当您按下事件视图控制器时,默认情况下将显示标题为“返回”的Back按钮,因此您无需手动更改它。但是,如果您决定更改Back按钮,则可以通过将UIBarButtonItem类型的新对象分配给导航项的backBarButtonItem属性来执行此操作。例如,您可以修改pushController:方法,以便在推送事件视图控制器之前为我们的根视图控制器提供自定义Back按钮。

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

    }

}

希望,这会有所帮助。

答案 1 :(得分:0)

当您按下EKEventViewController时,会分配对象但尚未加载视图。我发现的解决方案是使用UIAppearance API。请尝试以下电话。

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

不幸的是,这会影响应用中的所有UIBarbuttonItems,以下代码对我不起作用。因此,您可能必须手动设置UIBarbuttonItem的其他实例

此代码对我不起作用

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