简体   繁体   English

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

[英]How to change the position or hide magnifier icon in UISearchBar in IOS 7?

I am working on IOS 7 application.By default its appearing like Pic(1).But I need to change it as Pic(2).I googled and found few answers for the requirement,but it has not changed.Or else I need to hide.So that I can manage with background image.This is first image 我正在研究IOS 7应用程序。默认情况下它看起来像Pic(1)。但是我需要将其更改为Pic(2)。我用谷歌搜索并找到了几个答案的要求,但它没有改变。或者我需要隐藏。所以我可以用背景图像管理。这是第一张图片

照片(1)

在此输入图像描述

I used below code to modify it.But didnt succeed. 我使用下面的代码来修改它。但是没有成功。

In .h file 在.h文件中

@property(nonatomic,strong) IBOutlet UISearchBar *findSearchBar;

In .m file 在.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;
}

}

I am not getting how to make the center of the view's image to nil.Its really killing my time.Please help me.where I had gone wrong. 我没有得到如何使视图图像的中心为零。它真的杀了我的时间。请帮助我。我出错的地方。

    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;

Try this may be it will help u........ 试试这可能会对你有所帮助........

I'm not sure how to left-align the placeholder, but as of iOS 5.0 there's a simple, supported way to modify the search bar's text field properties, eg: 我不确定如何左对齐占位符,但从iOS 5.0开始,有一种简单的,支持的方式来修改搜索栏的文本字段属性,例如:

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setLeftViewMode:UITextFieldViewModeNever];

which will hide the magnifying glass icon. 这将隐藏放大镜图标。

你可以尝试:

searchBar.setImage(UIImage(named: "yourimage")!, forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Normal)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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