2013-10-24 46 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) 
相關問題