【问题标题】:IOS 7 Unable to change background image of search barIOS 7无法更改搜索栏的背景图片
【发布时间】:2014-06-06 17:55:15
【问题描述】:

我无法在我的 IOS 7 中自定义搜索栏。

我在 IOS6 中使用以下代码更改搜索栏背景

for (UIView * v in sarchBar.subviews)
{
    if ([v isKindOfClass:NSClassFromString(@"UISearchBarTextField")])
    {
        v.superview.alpha = 0;
        UIView *containerView = [[UIView alloc] initWithFrame:sarchBar.frame];
        [containerView addSubview:v];
        [self.view addSubview:containerView];
        [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]];
        [[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    }
}

其次,如果我自定义搜索控制器以删除搜索栏中的默认取消按钮,它在 IOS6 中工作正常,但使用自定义搜索控制器在 IOS7 中无法搜索。

【问题讨论】:

  • 您使用的是apple的私有类,请注意:您的应用可能会被iStore拒绝
  • 哪一个是私课?
  • 是的,我可以更改背景,但要完全自定义,您需要子类化 UISearchController 并且子类化后搜索不起作用
  • UISearchBarTextField 是私有的,根本没有文档,你可以只检查子视图来获取名称类,但这并不意味着你可以直接使用它。我告诉你这个,因为我曾经尝试过类似的东西,苹果拒绝了我的应用,这违反了苹果的规则……但你当然可以尝试……

标签: ios ios7 ios6 uisearchbar uisearchdisplaycontroller


【解决方案1】:

如果您使用文本字段并更改背景图像会更好,因为它是子类并继承相同的属性。

【讨论】:

    【解决方案2】:

    编辑:我可能误读了原始问题。下面的答案向您展示了如何从UISearchBar 中安全地获取UITextField

    回顾您的代码,您似乎在尝试直接修改UISearchBar 的视图层次结构。我知道苹果在 iOS 7 的其他 UI 元素(如 UIAlertView)上禁止这样做,但我不确定他们是否对 UISearchBar 做了同样的事情。

    关于取消按钮.. 你不能只使用UISearchBar 上的showsCancelButton 属性吗?


    我编写了以下类别来尝试从UISearchBar 中获取UITextField

    它适用于 iOS 7,但未在 6 上测试(我不明白为什么它不会)

    UISearchBar+BSSearchBar.h

    //
    // Created by Liam Nichols on 07/04/2014.
    // Copyright (c) 2014 Liam Nichols. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface UISearchBar (BSSearchBar)
    
    - (void)setTextColor:(UIColor *)color;
    
    - (UITextField *)internalTextField;
    
    @end
    

    UISearchBar+BSSearchBar.m

    //
    // Created by Liam Nichols on 07/04/2014.
    // Copyright (c) 2014 Liam Nichols. All rights reserved.
    //
    
    #import "UISearchBar+BSSearchBar.h"
    
    
    @implementation UISearchBar (BSSearchBar)
    
    - (void)setTextColor:(UIColor *)color
    {
        UITextField *textField = [self internalTextField];
        textField.textColor = color;
    }
    
    - (UITextField *)internalTextField
    {
        return [UISearchBar findTextFieldInView:self];
    }
    
    + (UITextField *)findTextFieldInView:(UIView *)view
    {
        for (id subview in view.subviews)
        {
            if ([subview isKindOfClass:[UITextField class]])
            {
                return subview;
            }
    
            UITextField *subviewTextField = [UISearchBar findTextFieldInView:subview];
            if ([subviewTextField isKindOfClass:[UITextField class]])
            {
                return subviewTextField;
            }
        }
        return nil;
    }
    
    @end
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      试试这个背景图片..我希望它有帮助

       for(UIView *subView in [searchBarObj subviews]) {
          for(UIView *subSubView in [subView subviews]) {
              if([subSubView conformsToProtocol:@protocol(UITextInputTraits)]) {
                  [(UITextField *)subSubView setBackgroundColor: [UIColor colorWithPatternImage:[UIImage imageNamed:@"imageName.png"]]];
              }
          }
        }  
      

      谢谢..

      【讨论】:

        猜你喜欢
        • 2011-07-10
        • 2016-02-18
        • 1970-01-01
        • 1970-01-01
        • 2015-01-16
        • 1970-01-01
        • 2013-11-01
        • 2012-11-05
        • 1970-01-01
        相关资源
        最近更新 更多