简体   繁体   中英

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:

在此输入图像描述

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.

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?

1) This is UI update problem,write this statement after any UI update code.

 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]];

this will update your GUI.

or

2) use perform selector on main thread and update your UI in selector method.

 [self performSelectorOnMainThread:@selector(UpdateUIMethod) withObject:self waitUntilDone:NO];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM