简体   繁体   English

无法更改自定义UIBarButton项目

[英]Cannot Alter Custom UIBarButton Item

I am trying to add a custom bar button to my project and I'm having a few problems. 我试图在我的项目中添加自定义栏按钮,但遇到了一些问题。 First lets go over the requirement for my button, it needs: 首先让我们回顾一下我的按钮的要求,它需要:

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

So I first tried connecting a barButtonItem up through the storyboard and I could change the colour and enable disable it. 因此,我首先尝试通过情节barButtonItem板上连接barButtonItem ,然后可以更改颜色并将其禁用。 However I could not change the font or add colour animation to it. 但是我无法更改字体或向其中添加颜色动画。 I want to be able to use the following method which I am already using in the app to animate the colour: 我希望能够使用已在应用程序中使用的以下方法来设置颜色的动画:

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

Ideally I would be able to change this function to accept a barButtonItem / UIButton so that I could animate it easily. 理想情况下,我可以更改此函数以接受barButtonItem / UIButton以便可以轻松对其进行动画处理。 However if I connect the button up through storyboards I cannot access the button's layer so the animation is not possible. 但是,如果我通过情节提要板将按钮连接起来,则无法访问按钮的图层,因此无法制作动画。

I had started to go about this through code but I've gone down 2 dead ends. 我已经开始通过代码来解决这个问题,但是我陷入了两个死胡同。 Here is a really basic implementation of a barButtonItem : 这是barButtonItem的真正基本实现:

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

Now this will work in the fact that I can use it like a storyboard button however I cannot alter the font or access the button's layer so it is also no good. 现在,这可以工作,因为我可以像情节提要按钮一样使用它,但是我不能更改字体或访问按钮的图层,因此也不好。

I then tried creating a fully custom view for my bar button by creating a UIView and a UIButton and then adding them to a barButtonItem , this can be seen here: 然后,我尝试通过创建UIViewUIButton并将其添加到barButtonItem来为我的bar按钮创建完全自定义的视图,可以在此处看到:

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;

Now I have a barButtonItem that will display a custom font however it is has no UI interaction at all. 现在,我有一个barButtonItem ,它将显示一个自定义字体,但是它根本没有UI交互。 It looks the same when enabled/disabled and when not-highlighted/highlighted. 启用/禁用和未突出显示/突出显示时,其外观相同。 The button works as it should it just doesn't look like it should. 该按钮可以正常工作,只是看起来不应该如此。

Should I create an outlet to this button somewhere in my code so that I can alter the button when in the view? 我是否应该在代码中的某个位置为此按钮创建一个插座,以便在视图中更改按钮? Is it even possible to alter a button after it has been added to the navigation bar? 将按钮添加到导航栏中后,甚至可以更改按钮吗?

If anybody has any suggestions it would be appreciated! 如果有人有任何建议,将不胜感激!

Yeah barbuttonitem isn't modifieable after you added it to the bar. 是的barbuttonitem在添加到栏中后不可修改。 only way i can think is to remove the button and add a new one (with the new characteristics) when user interact with it. 我能想到的唯一方法是删除按钮,并在用户与其交互时添加一个新按钮(具有新特性)。 I suggest you to use a custom toolbar (like a normal UIView), then add a normal button to it. 我建议您使用自定义工具栏(如普通的UIView),然后向其中添加普通的按钮。 it would be easier to customize 自定义会更容易

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM