2013-10-24 47 views
18

我正在使用IOS 7应用程序。默认情况下它显示为Pic(1)。但我需要将其更改为Pic(2)。我搜索了一下,发现需求的答案很少,但没有改变。或者其他我需要hide.So,我可以用背景image.This管理首先是图像如何在iOS 7的UISearchBar中更改位置或隐藏放大镜图标?

Pic(1)

enter image description here

我用下面的代码来修改它。但是didnt成功。

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

} 

我没有得到如何使视图的图像来nil.Its中心真是笑死我time.Please帮助我出错了。

+0

漱口迅速-3:http://stackoverflow.com/questions/19289406/uisearchbar-search-icon-isnot-left-aligned-in-ios7/41976067# 41976067 – Shrawan

回答

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; 

尝试,这可能是它会帮助ü........

+0

它的工作..谢谢:) – iSwaroop

+0

但它不工作,如果我应用RGB颜色 – iSwaroop

+0

尝试设置背景图像,而不是设置RGB颜色。 – Hari1251

9

我不知道如何左对齐的占位符,但由于安装iOS 5.0的有一个简单,支持方法来修改搜索栏的文本字段属性,例如:

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

这将隐藏放大镜图标。

+1

Upvote切割戈尔德结!为什么UISearchBar必须如此艰苦(修辞)? –

0

你可以尝试:

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