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

标签 ios iphone objective-c ios7 uisearchbar

我正在开发 IOS 7 应用程序。默认情况下它看起来像 Pic(1)。但我需要将它更改为 Pic(2)。我用谷歌搜索并找到了很少的需求答案,但它没有改变。或者否则我需要隐藏。以便我可以使用背景图片进行管理。这是第一张图片

Pic(1)

enter image description here

我用下面的代码修改了它,但是没有成功。

在.h文件中

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

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

}

我不知道如何使 View 图像的中心变为零。这真的很浪费我的时间。请帮助我。我哪里出错了。

最佳答案

    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;

试试这个可能对你有帮助.......

关于ios - 如何在 IOS 7 的 UISearchBar 中更改位置或隐藏放大镜图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19567131/

相关文章:

iphone - 我们可以向使用我们的SDK的应用发送推送通知吗?

objective-c - 迭代/循环 Objective C 中类的所有属性

ios - 我怎样才能把角标(Badge)图标放在菜单栏的应用程序中?

objective-c - Objective-C : How to use addTarget:action:forControlEvents: method?

iphone - MonoTouch : UpPopoverController. DidDismiss 未触发

iphone - arc4random 和 random 有什么区别?

Objective-C:获取日历年中的天数

objective-c - 仅通过 Objective-C 运行时函数使用 NSAutoreleasePool

iOS 低功耗蓝牙 (BLE) 未发现 TX 特性

ios - 将 Core Data 相关代码迁移到 Swift 2