C.Johns
C.Johns

Reputation: 10245

animate navigationbarbutton

I would like to know if there is a way to make the right, navigation bar button appear when say an if statement is qualified.. so say if the user is filling out some details and then the if statment runs is there a way to make the button go from not being there to fading in and being selectable?

currently I just have it sitting there, but would be a nice user reward to show that the user has meet the needs to press the search button etc.

this is currently all I am doing.

if (blaa blaa) {
//Add search barbutton
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Search" style:UIBarButtonSystemItemDone target:nil action:nil];
}

Upvotes: 0

Views: 262

Answers (3)

adali
adali

Reputation: 5977

first ,you should add the rightBarButtonItem to the navbar and set the alpha to 0.0

them try this

[UIView animateWithDuration:0.3f
                      delay:0
                    options:UIViewAnimationCurveEaseInOut
                 animations:^{
                     self.navigationItem.rightBarButtonItem.customView.alpha = 1.0f;

                 }
                 completion:^(BOOL finished) {
                 }];

update

in your code

if (blaa blaa) {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"search" forState:UIControlStateNormal];


    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView: btn] autorelease];

    self.navigationItem.rightBarButtonItem.customView.alpha = 0.0f;

}

then you can use the code above

update

sorry i checked the code , the UIViewControler will reset the barButtonItem property so you must set is agian before the view show

- (void)viewDidAppear:(BOOL)animated
{
    self.navigationItem.rightBarButtonItem.customView.alpha = 0.0f;
}

then when the button tapped

- (void) buttonTap:(UIButton*)btn
{
    [UIView animateWithDuration:1.0f
                      delay:0
                    options:UIViewAnimationCurveEaseInOut
                 animations:^{
                     self.navigationItem.rightBarButtonItem.customView.alpha = 1.0f;
                 }
                 completion:^(BOOL finished) {
                 }];
}

it should works!

Upvotes: 2

Mick MacCallum
Mick MacCallum

Reputation: 130193

An easy way to somewhat accomplish this would be to modify your buttons setEnable state. This will not change wether the button is visible but will toggle wether the user can interact with it, like so.

enter image description here

if (blahblah == YES) {
    [myBarButtonItem setEnabled:NO];

}else {
    [myBarButtonItem setEnabled:YES];
}

Upvotes: 1

CodaFi
CodaFi

Reputation: 43330

use -initWithCustomView to make a UIButton inside of a UIBarbuttonItem which in turn is inside the nav bar, PHEW! Then you have complete access to the alpha of the UIButton, a luxury UIBarButtonItem does not afford.

Of course, this is all pre iOS 5 mumbo jumbo. Use +appearance to animate your backgroundColor of the view from a 0.0 alpha to a 1.0 alpha (note, UIColor is not animateable in this case, use the CGColor property).

Upvotes: 1

Related Questions