开发者

Fading navigation controller title in/out

I have set 开发者_如何转开发my navigation controller's title with the title property. I would like the title to be able to fade in and out in response to some action performed by the user.

My current solution is to provide the navigation controller with a custom titleView, and I would animate the alpha change of that view whenever I want the title to fade. This works, but is creating another problem: I cannot center align the title since the left bar item and the right bar item are varied in width.

Therefore, I am looking for an alternate solution. Is there another way to fade the navigation controller title without using a custom title view?

EDIT: I have received some answers showing how to do this by adding subviews. Although that does enable the fade in/out feature I want, I would like to avoid using subviews as I ran into a problem when implementing that solution.

Is there something built into the navigation objects that allows the fading of the title? I have already done some research, and gauging form the responses thus far, I'm guessing there isn't. I'm just posting as a last resort, just incase anyone knows of a way.


Agreed with @ImaginaryCake comment: Tweaking the title property sounds like a hack. The proper way is to use the titleView instead of iterating through the UINavitationBar -subviews and hope it will survive SDK updates.

Quick & clean in only 2 steps ; From a UIViewController, do:

Step 1: Change the titleView for your navigationItem.

self.navigationItem.titleView = ({
    UILabel * titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    [titleView setTextAlignment:NSTextAlignmentCenter];
    [titleView setText:@"HeyHey"];

    titleView;
});

Step 2: Animate at will.

[UIView animateWithDuration:1.0f animations:^{
    self.navigationItem.titleView.alpha = 0.5f;
}];


It may be useful for u....

fade.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController
 {

     UINavigationBar *navigationbar;

     NSTimer * timer;
     UILabel * ll;
      UILabel * l2;
 }

@property(nonatomic,retain) UINavigationBar *navigationbar;


@end

fade.m

#define FADE_IN_RATE        2.0/100.0
#define FADE_OUT_RATE       2.0/200.0
- (void)viewDidLoad {   
    [super viewDidLoad];
    ll.alpha = 0.0;
    l2.alpha =1.0;
    ll = [[UILabel alloc]init];
    l2 = [[UILabel alloc]init];
    l2.backgroundColor = [UIColor grayColor];
    ll.text = @"Name";
    ll.backgroundColor = [UIColor grayColor];
    ll.frame = CGRectMake(115, 10, 60, 20);
    l2.frame = CGRectMake(115, 10, 60, 20);
    [self.navigationController.navigationBar addSubview:ll];
    [self.navigationController.navigationBar addSubview:l2];
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(fadeScreen) userInfo:nil repeats:NO];
}
- (void)fadeScreen
{

    timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(fadeTimerEvent) userInfo:nil repeats:YES];


}

- (void)fadeTimerEvent
{

    if (ll.alpha >= 2)
    {
                // At this point, layer1 is now visible  
        [timer invalidate];
        timer = nil;    

    }
    else
    { 

        // Fade lower layer in (increase alpha)
         ll.alpha += FADE_IN_RATE;

        // Fade upper layer out (decrease alpha)
          l2.alpha -= FADE_OUT_RATE;
    }
}

Regards, CNSivakumar


The title property of UIViewController is just a NSString. You need to add your custom view to your NavigationBar.

title

A localized string that represents the view that this controller manages.

@property(nonatomic, copy) NSString *title

Discussion

Subclasses should set the title to a human-readable string that represents the view to the user. If the receiver is a navigation controller, the default value is the top view controller’s title.


Quick and dirty: You should be able to add a custom label to the navigation bar via

[navigationBar addSubview:yourLabel];

Just setup the label's frame to be centered within the navigation bar and modify its alpha value as you did before. However, this is a real hack and I'm not sure how it behaves when you're pushing and popping view controllers.

EDIT:

Ok, here you go. First assume we have a navigation bar holding a navigation item with the title property (not the titleView property) as well as the left and right bar button item set.

UINavigationBar* bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:bar];

UINavigationItem* anItem = [[UINavigationItem alloc] initWithTitle:@"HeyHey"];
UIBarButtonItem* leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nil action:nil];
UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];
anItem.leftBarButtonItem = leftItem;
anItem.rightBarButtonItem = rightItem;
bar.items = [NSArray arrayWithObject:anItem];
[leftItem release];
[rightItem release];
[anItem release];

Now you can fade the title view like so:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[[[bar subviews] objectAtIndex:0] setAlpha:0];
[UIView commitAnimations];

I don't know if the title view is always at index 0, but you can check for your particular case using

for (UIView* aView in bar.subviews) {
    NSLog(@"%@", aView);
}

For the title view you will see something like

<UINavigationItemView: 0x7566530; frame = (0 0; 0 0); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x75665e0>>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜