iOS - 如何使导航栏的 subview 透明?

标签 ios uinavigationbar uisearchbar

我在为导航栏的某些 subview 设置 alpha 动画时遇到了一些麻烦。这是栏的初始状态:

enter image description here

这是我现在的进展情况:

enter image description here

问题是,当搜索处于事件状态时,我需要使所有这些“星形”、"new"和其他圆形按钮透明(我需要将它们的 alpha 降低为零)。我试图在委托(delegate)的以下方法中使它们透明:

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

在所有这些中我都做了完全相同的代码:

//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
  }
}

但是,我无法通过这种方式获得预期的结果,尽管我在该 NSLog 中获得了正确的 alpha 值,但按钮根本不褪色。关于我可能做错了什么有什么建议吗?

编辑 1:

我想我应该在我的案例中添加一些 View 层次结构的描述。好吧,我有 UISegmentedConrol,它包含这些圆形按钮以及两个 UIImageView、分段控件的背景和三角形指针。所有这些 View 都作为 subview 添加到导航栏中。还有一个矩形 View ,它包含搜索栏,稍后也作为 subview 添加到导航栏,因此它是层次结构中的顶层 View 。此外,我通过简单地为其 super View 的框架设置动画来为我的 searchBar 设置动画(当上述事件发生在委托(delegate)中时,展开或压缩它)。我希望我已经提供了所有必要的信息。

最佳答案

我已经有一段时间没有做 ios 编程了。有可能即使您正在设置 alpha,但不会触发重绘。 [UIView setNeedsDisplay] 可能有助于在下一个绘图周期刷新绘图。

供引用:What is the most robust way to force a UIView to redraw?

关于iOS - 如何使导航栏的 subview 透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15645576/

相关文章:

ios - UISearchBar - 内部 UITextField 高度问题

ios - 如何快速更改搜索栏的高度

ios - 在 iPad 上的 UIInterfaceOrientation 期间更改 UIView

ios - objective-c : Where to save the files in folder?

ios - 以编程方式快速更新约束

ios - UINavigationController 在顶部有额外的状态栏间隙

ios - 导航栏下推 View

ios - UINavigation后退栏按钮

iphone - 向 UITableView 子类添加属性

uisearchbar - iOS 7.1 UISearchBar 灰色 View