2013-10-24 14 views
18

Tôi đang làm việc trên ứng dụng IOS 7. Mặc dù nó xuất hiện như Pic (1). Nhưng tôi cần thay đổi nó thành Pic (2) .Tôi đã tìm được vài câu trả lời cho yêu cầu, nhưng nó không thay đổi. tôi cần phải hide.So mà tôi có thể quản lý với image.This nền là hình ảnh đầu tiênLàm thế nào để thay đổi vị trí hoặc ẩn biểu tượng kính lúp trong UISearchBar trong IOS 7?

Pic(1)

enter image description here

tôi đã sử dụng mã dưới đây để thay đổi nó.Nhưng didnt thành công.

Trong file .h

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

Trong 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; 
} 

} 

tôi không nhận làm thế nào để làm cho các trung tâm của hình ảnh của nhằm nil.Its thực sự giết chết sự giúp đỡ của tôi thời điểm này.Vui lòng me.where Tôi đã đi sai.

+0

Tham khảo cho nhanh-3: http://stackoverflow.com/questions/19289406/uisearchbar-search-icon-isnot-left-aligned-in-ios7/41976067# 41976067 – Shrawan

Trả lời

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; 

Hãy thử điều này có thể nó sẽ giúp u ........

+0

Nó hoạt động..Cảm ơn :) – iSwaroop

+0

Nhưng nó không hoạt động nếu tôi đang áp dụng màu RGB – iSwaroop

+0

cố gắng thiết lập hình ảnh nền thay vì thiết lập màu RGB cho nó. – Hari1251

9

Tôi không chắc chắn làm thế nào để trái align giữ chỗ, nhưng như của iOS 5.0 có một đơn giản, hỗ trợ cách sửa đổi các thuộc tính trường văn bản của thanh tìm kiếm, ví dụ:

sẽ ẩn biểu tượng kính lúp.

+1

Upvote để cắt nút gordian! Tại sao với UISearchBar phải rất gian khổ (hùng biện)? –

0

Bạn có thể thử:

searchBar.setImage(UIImage(named: "yourimage")!, forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Normal) 
Các vấn đề liên quan