如何在IOS 7中更改UISearchBar中的位置或隐藏放大镜图标?

iSw*_*oop 18 iphone objective-c uisearchbar ios ios7

我正在研究IOS 7应用程序.默认情况下它看起来像Pic(1).但是我需要将其更改为Pic(2).我用谷歌搜索并找到了几个答案的要求,但它没有改变.或者我需要隐藏.所以我可以用背景图像管理.这是第一张图片

照片(1)

在此输入图像描述

我使用下面的代码来修改它.但是没有成功.

在.h文件中

@property(nonatomic,strong) IBOutlet UISearchBar *findSearchBar;
Run Code Online (Sandbox Code Playgroud)

在.m文件中

@synthesize findSearchBar;

 - (void)viewDidLoad
 {
[super viewDidLoad];
 [self setSearchIconToFavicon];
 }

 - (void)setSearchIconToFavicon
{
// The text within a UISearchView is a UITextField that is a subview of that UISearchView.
   UITextField *searchField;
   for (UIView *subview in self.findSearchBar.subviews)
   {
       if ([subview isKindOfClass:[UITextField class]]) {
          searchField = (UITextField *)subview;
          break;
      }
  }

if (searchField)
{
    UIView *searchIcon = searchField.leftView;
    if ([searchIcon isKindOfClass:[UIImageView class]])
    {
        NSLog(@"aye");
    }
    searchField.rightView = nil;
    searchField.leftView = nil;
    searchField.leftViewMode = UITextFieldViewModeNever;
    searchField.rightViewMode = UITextFieldViewModeAlways;
}

}
Run Code Online (Sandbox Code Playgroud)

我没有得到如何使视图图像的中心为零.它真的杀了我的时间.请帮助我.我出错的地方.

小智 12

    UITextField *txfSearchField = [looksearchbar valueForKey:@"_searchField"];
    [txfSearchField setBackgroundColor:[UIColor whiteColor]];
    [txfSearchField setLeftViewMode:UITextFieldViewModeNever];
    [txfSearchField setRightViewMode:UITextFieldViewModeNever];
    [txfSearchField setBackground:[UIImage imageNamed:@"searchbar_bgImg.png"]];
    [txfSearchField setBorderStyle:UITextBorderStyleNone];
    //txfSearchField.layer.borderWidth = 8.0f;
    //txfSearchField.layer.cornerRadius = 10.0f;
    txfSearchField.layer.borderColor = [UIColor clearColor].CGColor;
    txfSearchField.clearButtonMode=UITextFieldViewModeNever;
Run Code Online (Sandbox Code Playgroud)

试试这可能会对你有所帮助........

  • 请注意,这可能会使您的应用从应用商店中被拒绝.特别是`[looksearchbar valueForKey:@"_ searchField"]`部分.更安全的方法是查看搜索栏的子视图(以及子视图的子视图),直到找到带有`[subview isKindOfClass:[UITextField class]]的正确子视图. (5认同)

att*_*cus 9

我不确定如何左对齐占位符,但从iOS 5.0开始,有一种简单的,支持的方式来修改搜索栏的文本字段属性,例如:

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setLeftViewMode:UITextFieldViewModeNever];
Run Code Online (Sandbox Code Playgroud)

这将隐藏放大镜图标.