【问题标题】:How to customize back barbutton on EKEventViewcontroller如何在 EKEvent Viewcontroller 上自定义后退栏按钮
【发布时间】:2013-07-22 02:35:50
【问题描述】:

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

    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;

【问题讨论】:

  • 看看这个问题的答案:Custom back button in UINavigationController.
  • 我已经尝试了该链接中的大部分答案。问题是我没有自定义视图控制器类,我使用苹果的 EKEventViewController 来创建视图控制器并推送它。我尝试创建一个继承 EKEventViewcontroller 的类,然后自定义 barbutton 但我无法让它工作。可能有人也可以在那里提供帮助吗?
  • 对于您的更新,请检查此。 stackoverflow.com/questions/6103748/…

标签: iphone uiviewcontroller ekevent childviewcontroller backbarbuttonitem


【解决方案1】:

发生的情况是,当您推送 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];

【讨论】:

    【解决方案2】:

    当你按下事件视图控制器时,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];
    
        }
    
    }
    

    希望,这会有所帮助。

    【讨论】:

    • 不起作用。这只会改变根视图控制器的左键,而不是我即将按下的 EKEventViewController 的后退按钮。
    • 我已经尝试过了,它对我有用。我已经从 Apple 网站下载了“EKEventViewController”的示例代码,并添加了一个带有图像的新后退按钮,它可以正常工作。
    • 我认为它是 SimpleEKDemo,我再次尝试了您的建议,得到了相同的结果。更改根控制器的leftbarbutton如何改变即将被推送的detail view controller的backbarbutton?
    • 是的,它是 SimpleEKDemo。让我再次检查一下我尝试过的内容,并尽快为您更新。
    • 最初,我误解了你的问题。但是现在,我得到了它并编辑了我的答案。请尝试并恢复。
    猜你喜欢
    • 2012-01-03
    • 1970-01-01
    • 2016-04-01
    • 2011-05-14
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    相关资源
    最近更新 更多