2011-08-09 47 views

Trả lời

11

Câu hỏi này được hỏi một lúc trước, do đó tôi giả định rằng người đã hỏi đã tìm được giải pháp. Nhưng chỉ trong trường hợp một số người khác xảy ra va chạm vào cùng một vấn đề. Đây là giải pháp của tôi.

Tôi có một nút UISearchBar với nút hủy xuất hiện chỉ khi trường văn bản của UISearchBar được khai thác. Giải pháp ghi đè - (void) layoutSubviews trong một lớp con của UISearchBar do đó không phải là một lựa chọn cho tôi. Dù sao tôi đã làm một phân lớp của UISearchBar (CustomSearchBar) với một phương pháp công cộng để thiết lập các phông chữ và textColor của nút hủy bỏ. Khi tôi tạo UISearchBar, tôi đảm bảo rằng ủy nhiệm textfield của thanh tìm kiếm được đặt thành self và lớp tạo thanh tìm kiếm, thực hiện giao thức UITextFieldDelegate. Khi người dùng chạm vào trường văn bản của thanh tìm kiếm, đại biểu của nó được thông báo và gọi phương thức của CustomSearchBar. Lý do tại sao tôi làm điều đó ở đây, là bởi vì đó là thời điểm khi nút hủy xuất hiện và do đó tôi biết rằng đó là trên hệ thống phân cấp khung nhìn và tôi có thể thực hiện tùy chỉnh của nó.

Dưới đây là các mã:

Để tạo UISearchBar trong MyRootViewController

CustomSearchBar *searchBar = [[CustomSearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40)]; 
[searchBar setBarStyle:UIBarStyleDefault]; 
[searchBar setTintColor:[UIColor whiteColor]]; 

for (UIView *view in [searchBar subviews]) 
{ 
    if ([view isKindOfClass:[UITextField class]]) 
    { 
     UITextField *searchTextField = (UITextField *)view; 
     [searchTextField setDelegate:self]; 
    } 
} 

self.searchBar = searchBar; 
[searchBar release]; 

UITextFieldDelegate trong MyRootViewController (chắc chắn rằng nó thực hiện các giao thức UITextFieldDelegate)

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    [self.searchBar setCloseButtonFont:[UIFont fontWithName:@"American Typewriter" size:14] textColor:[UIColor grayColor]]; 
} 

Và đây là công chúng trong lớp con của UISearchBar

- (void)setCloseButtonFont:(UIFont *)font textColor:(UIColor *)textColor 
{ 
    UIButton *cancelButton = nil; 

    for(UIView *subView in self.subviews) 
    { 
     if([subView isKindOfClass:[UIButton class]]) 
     { 
      cancelButton = (UIButton*)subView; 
     } 
    } 

    if (cancelButton) 
    { 
     /* For some strange reason, this code changes the font but not the text color. I assume some other internal customizations  make this not possible: 

     UILabel *titleLabel = [cancelButton titleLabel]; 
     [titleLabel setFont:font]; 
     [titleLabel setTextColor:[UIColor redColor]];*/ 

     // Therefore I had to create view with a label on top:   
     UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(2, 2, kCancelButtonWidth, kCancelButtonLabelHeight)]; 
     [overlay setBackgroundColor:[UIColor whiteColor]]; 
     [overlay setUserInteractionEnabled:NO]; // This is important for the cancel button to work 
     [cancelButton addSubview:overlay]; 

     UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, kCancelButtonWidth, kCancelButtonLabelHeight)]; 
     [newLabel setFont:font]; 
     [newLabel setTextColor:textColor]; 
     // Text "Cancel" should be localized for other languages 
     [newLabel setText:@"Cancel"]; 
     [newLabel setTextAlignment:UITextAlignmentCenter]; 
     // This is important for the cancel button to work 
     [newLabel setUserInteractionEnabled:NO]; 
     [overlay addSubview:newLabel]; 
     [newLabel release]; 
     [overlay release]; 
    } 
} 
+0

Điều này thật tuyệt vời. Cảm ơn rất nhiều vì chuyện này. Tôi đã sử dụng một phương pháp thay thế mà tôi không hài lòng lắm. Nhưng với điều này tôi có thể tái thăm vấn đề này ngay bây giờ. Cảm ơn! – Zhen

+0

Tôi rất vui vì nó đã giúp bạn. – strave

2

Bạn có thể phân lớp UISearchBar và viết phương pháp - (void)layoutSubviews của riêng bạn. Trong phương thức này, lặp qua các phần con của nó và lấy nút cancelButton. Phần còn lại nên thẳng về phía trước.

+0

cảm ơn câu trả lời. Tuy nhiên tôi không quen thuộc với cách tôi có thể viết lại phương thức layoutSubView.Bạn có thể giúp hướng dẫn tôi về điều này không? – Zhen

+0

Ghi đè - (void) bố cụcSubviews sẽ chỉ hoạt động nếu thuộc tính của UISearchBar hiển thịCancelButton được đặt thành CÓ. Theo mặc định, giá trị này được đặt thành NO và do đó, nút hủy chỉ xuất hiện khi trường văn bản của UISearchBar được nhấn. Trong trường hợp này ghi đè bố cụcSubviews sẽ không có hiệu lực. – strave

7

Thay vì làm tất cả những điều này ưa thích chỉ cần thực hiện SearchBarTextDidBeginEditing như thế này

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 
{ 
    // only show the status bar’s cancel button while in edit mode sbar (UISearchBar) 
    searchBar.showsCancelButton = YES; 
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo; 
    UIColor *desiredColor = [UIColor colorWithRed:212.0/255.0 green:237.0/255.0 blue:187.0/255.0 alpha:1.0]; 


    for (UIView *subView in searchBar.subviews){ 
     if([subView isKindOfClass:[UIButton class]]){ 
      NSLog(@"this is button type"); 

      [(UIButton *)subView setTintColor:desiredColor]; 
      [(UIButton *)subView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
     } 
} 
+0

Điều này làm việc như một sự quyến rũ! – Giovanni

4

câu trả lời Gyanerdra của hoạt động tốt. Nhưng đối với iOS7, tôi cần phải thực hiện thay đổi sau để nó hoạt động trong ứng dụng của tôi.

NSArray *childViews; 
if ((APP).isIOS7) { 
    childViews = [[searchBar.subviews objectAtIndex:0] subviews]; 
} else { 
    childViews =searchBar.subviews; 
} 

for (UIView *subView in childViews) { 
    if([subView isKindOfClass:[UIButton class]]){ 
     [(UIButton *)subView setTintColor:desiredColor]; 
     [(UIButton *)subView setTitleColor:desiredColor forState:UIControlStateNormal]; 
    } 
} 

Dường như đối với iOS7 thanh tìm kiếm được đặt trong chế độ xem gốc. Hy vọng điều này sẽ giúp ai đó. b

0

Bạn có thể tận dụng Thuộc tính thời gian chạy iOS _cancelButton để đạt được điều này.

UIButton *cancelButton = [searchBar valueForKey:@"_cancelButton"]; 
[cancelButton setTitleColor:[UIColor yourColor] forState:UIControlStateNormal]; 

Unable to change UISearchBar cancel button title color after changing it's text.

+1

Nhận cancelButton = nil cho đến khi bạn không bật tính năng tìm kiếmBar.showsCancelButton = YES – kb920

0

KVC

UIButton * nút = [_searchBar valueForKey: @ "_ cancelButton"]; button.titleLabel.font = [Hệ thống UIFontFontOfSize: 13];

+1

Điều này có vẻ như nó ảnh hưởng đến kích thước phông chữ, không phải màu. – ouflak

+0

Vì vậy, u có thể thay đổi phông chữ, u cũng có thể thay đổi titleColor. [button setTitleColor: [UIColor orangeColor] forState: UIControlStateNormal]; – fengbai

Các vấn đề liên quan