Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fade UIBarButtonItem When Swapped

When I change my UIBarButtonItems they change abruptly, unlike the default which gives a nice but speedy fade animation. You can see this when segueing between view controllers, for instance, the back button will fade in and out. How can I simulate the same effect?

like image 295
Michael Mangold Avatar asked Mar 08 '14 13:03

Michael Mangold


1 Answers

Update - Based on this answer - https://stackoverflow.com/a/10939684/2649021

It looks like you would have to do something like this to make the button itself fade out.

[self.navigationItem setRightBarButtonItem:nil animated:YES];

And do something like this to make it fade in

 [self.navigationItem setRightBarButtonItem:myButton animated:YES]; 

Otherwise if you want more control over animation properties you would have to create a custom view I believe.

EDIT: I just confirmed that you can fade a UIBarButtonItem custom view using this.

As a test I created a simple project and dropped a UIBarButtonItem onto the navigation bar. I created an outlet to the view controller. In viewDidLoad on the view controller I setup a custom view

-(void)viewDidLoad{
    [super viewDidLoad];
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0,0,40,40)];
    lbl.text = @"test";
    UIView *customView = [[UIView alloc]initWithFrame:CGRectMake(0,0,40,40)];
    [customView addSubview:lbl];
    self.barButtonItem.customView = customView;
}

As a test in viewDidAppear I animated it

 -(void)viewDidAppear:(BOOL)animated
{
    [UIView animateWithDuration:3.0 
                          delay:3.0 
                        options:UIViewAnimationOptionCurveEaseOut        
                            animations:^{
                                self.barButtonItem.customView.alpha = 0;
                   } 
                     completion:^(BOOL finished) {
                                NSLog(@"animation complete");
}];

EDIT: Here's a link to Apples Documentation for a full explanation of UIView animations. https://developer.apple.com/library/ios/documentation/windowsviews/conceptual/viewpg_iphoneos/animatingviews/animatingviews.html

like image 140
digitalHound Avatar answered Nov 06 '22 15:11

digitalHound