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:
Here is how far did I get by now:
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.
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.
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 .
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);
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With