2011-08-09 14 views

उत्तर

11

इस सवाल को कुछ समय पहले पूछा गया था, इसलिए मुझे लगता है कि जिस व्यक्ति ने पूछा है उसे पहले ही समाधान मिला है। लेकिन अगर कुछ अन्य एक ही समस्या में टक्कर मारने लगते हैं। मेरा समाधान यहाँ है।

मेरे पास एक रद्द बटन के साथ एक UISearchBar है जो केवल तब दिखाई देता है जब UISearchBar का टेक्स्टफील्ड टैप किया जाता है। ओवरराइडिंग का समाधान - (शून्य) लेआउट UISearchBar के उप-वर्ग में सबव्यूव्यू इसलिए मेरे लिए एक विकल्प नहीं था। वैसे भी मैंने रद्द बटन के फ़ॉन्ट और टेक्स्ट को स्थापित करने के लिए सार्वजनिक विधि के साथ UISearchBar (कस्टमशर्चबार) का उप-वर्ग किया था। जब मैं UISearchBar बनाता हूं, तो मैं सुनिश्चित करता हूं कि खोज बार का टेक्स्टफील्ड प्रतिनिधि स्वयं को सेट किया गया है और क्लास जो खोज बार बनाता है, UITextFieldDelegate प्रोटोकॉल लागू करता है। जब उपयोगकर्ता खोज बार के टेक्स्टफील्ड पर टैप करता है, तो उसके प्रतिनिधि को सूचित किया जाता है और कस्टमशर्चबार की विधि को कॉल किया जाता है। कारण मैं इसे यहां क्यों करता हूं, क्योंकि यह क्षण है जब रद्द बटन दिखाई देता है और इसलिए मुझे पता है कि यह दृश्य पदानुक्रम पर है और मैं इसका अनुकूलन कर सकता हूं।

कोड यह रहा:

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 MyRootViewController में UISearchBar बनाने के लिए (यकीन है कि यह UITextFieldDelegate प्रोटोकॉल लागू करता है बनाने के)

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

और यह सार्वजनिक है 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

यह कमाल है। इसके लिए बहुत - बहुत धन्यवाद। मैंने एक वैकल्पिक विधि का उपयोग किया जो मैं बहुत खुश नहीं हूं। लेकिन इसके साथ मैं अब इस मुद्दे पर फिर से जा सकता हूं। धन्यवाद! – Zhen

+0

मुझे खुशी है कि यह आपकी मदद करता है। – strave

2

आप UISearchBar उपवर्ग और अपने खुद के - (void)layoutSubviews विधि लिख सकते हैं। इस विधि में, इसके सबव्यू के माध्यम से लूप करें और रद्द करें बटन प्राप्त करें। बाकी सीधे आगे होना चाहिए।

+0

उत्तर के लिए धन्यवाद। हालांकि मैं लेआउटसब्यूवी विधि को फिर से लिखने के तरीके से बहुत परिचित नहीं हूं।क्या आप इस पर मुझे मार्गदर्शन करने में मदद कर सकते हैं? – Zhen

+0

ओवरराइडिंग (शून्य) लेआउट सबव्यूव्यू केवल तभी काम करेंगे जब UISearchBar की संपत्ति दिखाती है CancelButton YES पर सेट है। डिफ़ॉल्ट रूप से यह NO पर सेट होता है और इसलिए, रद्द बटन केवल UISearchBar के टेक्स्टफील्ड हिट होने के बाद दिखाई देता है। इस मामले में लेआउट ओवरराइडिंग सेव्यूव्यू का कोई प्रभाव नहीं पड़ेगा। – strave

7
इसके बजाय इन सभी फैंसी काम करने के

सिर्फ इस

- (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

यह एक आकर्षण की तरह काम किया! – Giovanni

4

Gyanerdra का जवाब अच्छी तरह से काम करता है की तरह SearchBarTextDidBeginEditing लागू। लेकिन आईओएस 7 के लिए मुझे इसके ऐप में काम करने के लिए निम्नलिखित बदलाव करने की जरूरत है।

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

ऐसा लगता है कि आईओएस 7 के लिए खोज बार एक मूल दृश्य में संलग्न है। उम्मीद है कि यह किसी की मदद करेगा। बी

0

आप इसे प्राप्त करने के लिए आईओएस रनटाइम संपत्ति _cancelButton का लाभ उठा सकते हैं।

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

रद्द करनाबटन = शून्य जब तक आप खोज करने में सक्षम नहीं होते हैं। Bar.showsCancelButton = YES – kb920

0

KVC

UIButton * बटन = [_searchBar valueForKey: @ "_ cancelButton"]; बटन.titleLabel.font = [यूआईएफओटी सिस्टमफॉन्टऑफसाइज: 13];

+1

ऐसा लगता है कि यह फ़ॉन्ट आकार को प्रभावित करता है, रंग नहीं। – ouflak

+0

तो, आप फ़ॉन्ट बदल सकते हैं, आप शीर्षक रंग बदल सकते हैं। [बटन सेट टिटल रंग: [यूआईसीओलर नारंगी रंग] फॉरस्टेट: यूआईसींट्रोलस्टेट नॉर्मल]; – fengbai

संबंधित मुद्दे