4

So I am using the UINavigationBar component of iOS7, but not the UINavigationController itself. When I push a new view with my custom navigation controller, I want to change the title's alpha to 0.0 and then back, but I can't seem to get it work.

I am trying to do this

vc1.navBar.topItem.titleView.alpha = 0.1;

It doesn't seem to have any effect. Am I missing something here, is there a correct way to achieve this?

3 Answers 3

6

This is how I would do it:

  • In viewWillAppear: of the pushed viewController, I would set it the alpha to 0 using:

    [self.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                [[UIColor whiteColor] colorWithAlphaComponent:0],
                                                                NSForegroundColorAttributeName,
                                                                [UIFont fontWithName:@"Helvetica-Bold" size:16.0],
                                                                NSFontAttributeName,
                                                                 nil]];
    
  • In viewDidAppear:, set it's alpha back to 1. Here you don't really need to set it's alpha to 1, just setting it to plain [UIColor whiteColor] will do the trick.

One other thing you could do is, toggle HIDDEN property in the 2 methods.

Setting the alpha of the titleView did not work in my case so I used the above code. It hides the title. In case you want a similar effect with the back button, you set the alpha of the backButton using self.navigationItem.backBarButtonItem.customView.alpha =

2
  • What if I use custom titleView ?
    – onmyway133
    Oct 14, 2014 at 15:26
  • Then you set the alpha of the custom view Oct 14, 2014 at 15:34
5

You can set the titleView hidden in viewWillAppear

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.navigationItem.titleView.hidden = YES;
}

You can't set the titleView alpha until viewDidAppear

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    self.navigationItem.titleView.alpha = 0.5f;
}
0

You can try this one -

[self.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                    [UIColor colorWithRed:125/255.0 green:178/255.0 blue:15/255.0 alpha:0.2],
                                                                    NSForegroundColorAttributeName,
                                                                    [UIFont fontWithName:@"Helvetica-Bold" size:16.0],
                                                                    NSFontAttributeName,
                                                                     nil]];

If you just want to change the title attributes. Or this one to change bar alpha :

[self.navigationBar setBarTintColor:[UIColor colorWithRed:125/255.0 green:178/255.0 blue:15/255.0 alpha:0.2]];
1
  • 1
    Is there a way to animate the alpha change? Jan 1, 2014 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.