【问题标题】:Objective C - Altering appearance of control implementing appearance apiObjective C - 改变实现外观 api 的控件的外观
【发布时间】:2014-06-24 08:19:11
【问题描述】:

我正在使用外观api自定义UISearchBar

[[UISearchBar appearance] setSearchFieldBackgroundImage:searchBackground forState:UIControlStateNormal];
[[UISearchBar appearance] setBackgroundImage:backgroundImage];
[[UISearchBar appearance] setScopeBarBackgroundImage:backgroundImage];
[[UISearchBar appearance] setBackgroundColor:[UIColor clearColor]];

我在一个作为其他类的父类的类中使用它,以便在所有 child 类上实现效果。

现在我需要自定义一个包含UISearchBar 的子类。我试过link

但是当外观在超类上完成时,我无法获得这种效果。如果我删除 appearance 那么我就能产生这种效果。

如何通过实现 appearance api 来做到这一点?

【问题讨论】:

    标签: ios objective-c uisearchbar uiappearance


    【解决方案1】:

    基于Apple's documentation

        [[UISearchBar appearanceWhenContainedIn:[yourSubClassedViewControllerWhichHasSearchbar class], nil]
         setBackgroundColor:[UIColor orangeColor]];
    

    【讨论】:

      【解决方案2】:

      我用下面的代码解决了这个问题

      [[UISearchBar appearance] setSearchFieldBackgroundImage:nil forState:UIControlStateNormal];
      [[UISearchBar appearance] setBackgroundImage:nil];
      [[UISearchBar appearance] setBackgroundColor:[UIColor clearColor]];
      
      for (id img in srchbar.subviews) {
          if ([img isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
              [img removeFromSuperview];
          }
      }
      

      编辑: 以上解决方案可能会导致其他显示错误。所以最好的解决方案被标记为正确的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-02
        • 1970-01-01
        • 2012-02-04
        • 2012-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多