2013-12-17 184 views
0

我自定義我的搜索欄,但它沒有顯示任何圖像。它也會刪除其默認圖像。自定義搜索欄searchIcon

UITextField *searchField = nil; 
    for (UIView *subview in [[workLocationSearchBar.subviews lastObject] subviews]) { 
     NSLog(@"hey"); 
     if ([subview isKindOfClass:[UITextField class]]) { 
      searchField = (UITextField *)subview; 
      NSLog(@"inside"); 
      break; 
     } 
    } 

    if (searchField) { 
     UIView *searchIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"circle.png"]]; 
     if ([searchIcon isKindOfClass:[UIImageView class]]) { 

     } 
     NSLog(@"aye"); 
     searchField.rightView = searchIcon; 
     searchField.leftView=searchIcon; 

    } 
+0

請結帳這個http://stackoverflow.com/questions/9794552/ios-change-icon-in-the-search-bar – Nassif

+0

你UIView的分配,而不是UIImageView.Please進行更改並嘗試它。 – Nassif

+0

它對您有幫助嗎? – Nassif

回答

1

試試這個:

[workLocationSearchBar setImage:[UIImage imageNamed:@"IMAGE_NAME"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];