Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - how to make subviews of a Navigation Bar transparent?

I've been experiencing some troubles with animating the alpha of certaing subviews of my Navigation Bar. Here is the initial state of the bar:

enter image description here

Here is how far did I get by now:

enter image description here

The thing is that I need to make all this "star", "new" and other round buttons transparent when the search is active (I need to reduce their alpha to zero). I am trying to make them transparent in the following methods of the delegate:

- (void)searchBarCancelButtonClicked:(UISearchBar *)asearchBar
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar

In all of them I do quite the same code:

//parent is just a pointer of a search delegate, which points to view controller

for (UIView * someViewToFade in parent.navigationController.navigationBar.subviews) {
  if (someViewToFade.tag == 88 || someViewToFade.tag == 11) {
    NSLog("someViewToFade.alpha before changes = %f", someViewToFade.alpha);
    someViewToFade.alpha = 0.0; //or 1.0, depending on the method
  }
}

However, I couldn't achieve the desired result this way, though I am getting right alpha values at that NSLog, buttons don't fade at all. Are there any suggestions on what can I possibly be doing wrong?

EDIT 1:

I think I should add some description of the view hierarchy in my case. Well, I have the UISegmentedConrol, which holds these round buttons and also two UIImageViews, the background for segmented control, and the triangle pointer. All of these views are added to the navigation bar as it's subviews. Also there is a rectangular view, which holds the searchbar, and is also added as a subview to the navigation bar later, so it's the top view in the hierarchy. Also, I am animating my searchBar by simply animating the frame of its superview (expanding or condensing it, when the events above occur in the delegate). I hope I've given all the necessary information.

like image 578
morgan1189 Avatar asked Mar 26 '13 19:03

morgan1189


People also ask

How do I make my navigation bar transparent?

Creating a transparent navbar is very easy - just don't add a color class . bg-* to the navbar. In this case, the Navbar will take the color of the parent's background color.

How do you make the navigation bar transparent in Swift iOS?

You need to do three things to make a navigation bar transparent. Set background image to non-nil empty image ( UIImage() ). Set shadow image to non-nil empty image ( UIImage() ). Set isTranslucent to true .

What is translucent navigation bar?

Translucent or partially transparent is when the colour can be seen but the background behind the object is also slightly visible through it. For this we use. background-color: #FFFFFF; opacity: 0.5; filter: alpha(opacity=50);

How do I change the color of my UINavigationBar?

Open the project's storyboard file. Select the UINavigationBar from your UINavigationController scene. In the Attributes Inspector pane turn on these Appearances: “Standard”, “Compact”, “Scroll Edge”, and “Compact Scroll Edge”. For all four appearances, set the “Background” to “System Red Color”, for example.


1 Answers

Its been a while since I did ios programming. It is possible that even though you are setting the alpha, but not triggering a repaint. [UIView setNeedsDisplay] might help to refresh the drawing in the next drawing cycle.

For a reference: What is the most robust way to force a UIView to redraw?

like image 145
ssinganamalla Avatar answered Oct 02 '22 01:10

ssinganamalla