【问题标题】:Cannot Alter Custom UIBarButton Item无法更改自定义 UIBarButton 项
【发布时间】:2018-05-22 22:15:45
【问题描述】:

我正在尝试将自定义栏按钮添加到我的项目中,但遇到了一些问题。首先让我们回顾一下我的按钮的要求,它需要:

- to display a custom font
- to be enabled/disabled
- to have its colour animated based to provide user feedback.

所以我首先尝试通过情节提要连接barButtonItem,然后我可以更改颜色并启用禁用它。但是我无法更改字体或为其添加彩色动画。我希望能够使用我已经在应用程序中使用的以下方法来为颜色设置动画:

- (void)textFlashTextfieldAnimation:(nonnull UITextField *)view duration:(NSTimeInterval)duration animateToColour:(UIColor*)animationColour textColor:(UIColor*)textColour completion:(void (^)(BOOL finished))completion {
    [UIView transitionWithView:view duration:0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        view.textColor = animationColour;
    } completion:^(BOOL finished) {
        [UIView transitionWithView:view duration:2.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
            view.textColor = textColour;
        } completion:completion];
    }];
} 

理想情况下,我可以更改此函数以接受 barButtonItem / UIButton 以便我可以轻松地对其进行动画处理。但是,如果我通过情节提要连接按钮,我将无法访问按钮的图层,因此无法播放动画。

我已经开始通过代码来解决这个问题,但我已经走入了 2 个死胡同。这是barButtonItem 的一个非常基本的实现:

UIBarButtonItem *myButton=[[UIBarButtonItem alloc] initWithTitle:@"My Custom Button" style:UIBarButtonItemStylePlain target:self action:@selector(/*do something*/)];
    [self.navigationItem setRightBarButtonItem:myButton];

现在这将起作用,因为我可以像使用故事板按钮一样使用它,但是我无法更改字体或访问按钮的图层,所以它也不好。

然后我尝试通过创建UIViewUIButton 然后将它们添加到barButtonItem 来为我的栏按钮创建一个完全自定义的视图,这可以在这里看到:

UIFont *barButtonFonts = [UIFont fontWithName:kFont size:16];

    //Right Button
    //create view
    UIView *rightButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 45, 25)];

    //create button
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    rightButton.backgroundColor = [UIColor clearColor];
    rightButton.frame = rightButtonView.frame; //set frame = to view
    [rightButton setTitle:NSLocalizedString(@"Save", nil) forState:UIControlStateNormal];
    rightButton.titleLabel.font = barButtonFonts;

    rightButton.tintColor = [UIColor redColor];

    [rightButton addTarget:self action:@selector(actionMethod) forControlEvents:UIControlEventTouchUpInside];

    [rightButtonView addSubview:rightButton];

    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightButtonView];

    self.navigationItem.rightBarButtonItem = rightBarButton;

现在我有一个barButtonItem,它将显示自定义字体,但它根本没有 UI 交互。启用/禁用和未突出显示/突出显示时看起来相同。该按钮可以正常工作,只是看起来不应该。

我是否应该在代码中的某处为该按钮创建一个出口,以便在视图中更改该按钮?是否可以在将按钮添加到导航栏后对其进行更改?

如果有人有任何建议,将不胜感激!

【问题讨论】:

    标签: ios objective-c uibutton uibarbuttonitem


    【解决方案1】:

    是的,barbuttonitem 在您将其添加到栏后不可修改。我能想到的唯一方法是在用户与之交互时删除按钮并添加一个新按钮(具有新特性)。我建议您使用自定义工具栏(如普通 UIView),然后向其添加普通按钮。定制会更容易

    【讨论】:

    • 是的,我最终明白了这一点。我希望使用barButtoncustomView 属性来做一些动画(实际上我可以为背景设置动画),但没有其他方法可以工作。我现在刚刚为按钮设置了正常的启用/选择/禁用状态,它可以工作。
    猜你喜欢
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 2011-03-19
    相关资源
    最近更新 更多