1
votes

I've initialized a UIBarButtonItem with a custom UIButton and set it to be the rightBarButtonItem within a navigation controller.

UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightNavigationButton];//rightNavigationButton is the custom UIButton I set up before

self.navigationItem.rightBarButtonItem = rightBarButtonItem;

Via the appearance proxy of UIButton I set up the colours for the UIControlStates (Normal/Disabled/Highlighted). The UIBarButtonItem is behaving as expected.

When a "save-button" is pressed, I want to highlight the UIBarButtonItem to give a visual feedback that something has been saved. What I'm trying to do is to simulate highlighting of the UIBarButtonItem through just changing the color of the title during the saving animation since it doesn't seem that can trigger the highlighting animation (or set a selected property) programmatically (also mentioned in this post)

So what I did is set a IBOutlet property, hooked it up to the UIBarButtonItem in InterfaceBuilder and assigned the rightBarButtonItem to it. When the "save-button" is pressed I'm trying this:

    [UIView animateWithDuration:0.5f
                      delay:0.0f
                    options:UIViewAnimationCurveEaseOut
                 animations:^{
                           //some other animation code here
                          [self.barButtonItemOutlet setTitleTextAttributes:[NSDictionary            dictionaryWithObjectsAndKeys:
                          [UIFont fontWithName:FONT_ICON size:FONTSIZE],UITextAttributeFont,
                          HIGHLIGHT_COLOUR,UITextAttributeTextColor,
                          [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],UITextAttributeTextShadowOffset,
                          [UIColor colorWithRed:0 green:0 blue:0 alpha:0.0],UITextAttributeTextShadowColor,nil]
                          forState:UIControlStateNormal];

                 }
                 completion:nil
   ];

But there is nothing happening at all. The only way I got it to somehow work was to disable/enable it. But since colouring for disabling and highlighting should be different and I need both of these states this is not really an option.

Any help is appreciated!

1
Check if you can change the alpha color of title from 0 to 1 in animation block.sridevi
yup, I can change the alpha channel. (Reinhard also pointed out that this prop is animatable) But that doesn't really help with my colour changing problem. Am I missing something?codingPanda

1 Answers

1
votes

Well, the docs of UIView class say: The following properties of the UIView class are animatable:

  @property frame  
  @property bounds  
  @property center  
  @property transform  
  @property alpha  
  @property backgroundColor  
  @property contentStretch  

So the color cannot be animated, sorry.

EDIT: My answer concerned UIView animations, but there are also CALayer animations which are more flexible.

I found this link, and maybe you can also animate the color if you use CALayer animation. Let me look into it.
EDIT: Well I found this very useful book chapter on core animation. Unfortunately It does not seem to solve your problem, since the title color of a UIBarButtonItem seems not to be a property of a CALayer that could be animated. Sorry, I quit...