2013-10-08 45 views
8

裁剪在iOS系統7的UISearchBar佔位符中心對齊和重疊的書籤按鈕,直到不選擇搜索欄:的UISearchBar佔位符對準和在IOS 7

enter image description here

當選擇,它看起來如預期:

enter image description here

我需要它看起來這樣所有的時間。謝謝。

+0

請分享一些代碼。另外,請確保向Apple提交錯誤報告。 –

+0

你需要的任何字體大小 – codercat

+0

什麼是你的UISearchBar幀大小 – codercat

回答

-2

我不是在這裏給你一個通用的解決方案,但如果你有一個佔位符添加,做最愚蠢的和簡單的方法是自己截斷佔位符,所以不是

searchBar.placeholder = @「woord hier invoeren」;

讓它成爲

searchBar.placeholder = @ 「woord票數INVO ......」;

我試圖搞砸蘋果的私人方法,但沒有運氣: searchBar子視圖是: -UISearchBarBackground。 -UISearchBarTextField。 將UISearchBarBackground放在一邊 UISearchBarTextField實例的子視圖是: -_UISearchBarSearchFieldBackgroundView。 -UIImageView。 -UISearchBarTextFieldLabel。

我去的是試圖混淆的矩形(我強調字混亂,因爲這些都是私人方法)的UISearchBarTextFieldLabel因爲我敢肯定,它的框架是不正確的,當searchBar按鈕是顯示(書籤),如果你選擇searchBar.showsBookmarkButton = NO; placeHolder文本將按預期截斷。 這取決於你,有時候可以節省自己的時間,並用愚蠢的解決方案去做,但這樣做可以完成任務,或者進一步深入研究。 保持良好的工作。

3

新的解決方案:

// 
// WPViewController.m 
// test 
// 
// Created by VASANTH K on 02/01/14. 
// 
// 

    #import "WPViewController.h" 

    @interface WPViewController() 
    { 
     UILabel *lableCopy; 
    } 

    @end 

    @implementation WPViewController 

    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     //[self fixSearchBar:searchBar]; 
     // Do any additional setup after loading the view, typically from a nib. 

     self.searchBar.delegate=self; 
    } 

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 

     [self.searchBar resignFirstResponder]; 
     //[self fixSearchBar:searchBar]; 
    } 
    -(void)viewDidAppear:(BOOL)animated 
    { 
     [super viewDidAppear:animated]; 

     [self fixSearchBar:self.searchBar]; 

    } 

    - (void)didReceiveMemoryWarning 
    { 
     [super didReceiveMemoryWarning]; 
     // Dispose of any resources that can be recreated. 
    } 


    -(void)searchBarTextDidBeginEditing:(UISearchBar *)search 
    { 
     [self fixSearchBar:self.searchBar]; 
    } 

    -(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar 
    { 
     [self fixSearchBar:self.searchBar]; 
    } 

    -(void)fixSearchBar:(UISearchBar*)searchBar 
    { 
     UITextField *searchField = [searchBar valueForKey:@"_searchField"]; 

     // [searchField setValue:[UIColor blueColor] forKeyPath:@"_placeholderLabel.textColor"]; 

     UILabel *lable=[searchField valueForKey:@"_placeholderLabel"]; 

     if(!lableCopy) 
     { 
      lableCopy=[[UILabel alloc]initWithFrame:lable.frame]; 
      lableCopy.font=lable.font; 
      [lableCopy setText:lable.text]; 
      [lableCopy setTextColor:lable.textColor]; 
      UIButton *button; 

      for (UIView *view in [[[[searchBar.subviews objectAtIndex:0] subviews] objectAtIndex:1] subviews]) { 
       if([view isKindOfClass:[UIButton class]]) 
       { 
        button=(UIButton*)view; 
        break; 
       } 
      } 



      if(button) 
      { 
       //lable.hidden=YES; 
       CGRect newFrame=lable.frame; 
       newFrame.size.width=button.frame.origin.x-lable.frame.origin.x; 
       lableCopy.frame=newFrame; 
       [lableCopy adjustsFontSizeToFitWidth]; 
       //lableCopy.backgroundColor=[UIColor blackColor]; 
       [searchField addSubview:lableCopy]; 
       lableCopy.text=lable.text; 
       //lableCopy.textColor=[UIColor redColor]; 
      } 

     } 
     for (UIView *view in [[searchBar.subviews objectAtIndex:0] subviews]) { 
      if([view isKindOfClass:[UITextField class]]) 
      { 
       // NSLog(@"%@",view); 
       NSLog(@"TextFieldPresent==>%@",view); 
       if([view isFirstResponder]) 
       { 
        lable.hidden=NO; 
        lableCopy.hidden=YES; 
       } 
       else 
       { 
        lable.hidden=YES; 
        lableCopy.hidden=NO; 
       } 
       break; 
      } 
     } 

    } 


    @end 

這種解決方案只是增加新的UILable視圖並隱藏現有的佔位符給searchBar.Again的真實感受重新顯示實際佔位符,當搜索欄開始活躍起來。

這可能是修復IOS7中UI問題的一種臨時手段。

When Inactive

When Active

OLD SOLUTION: [searchField的setValue:[NSNumber的numberWithBool:YES] forKeyPath:@ 「_ placeholderLabel.adjustsFontSizeToFitWidth」];

在ios7中將不起作用,因爲用於disaplay內容的標籤大小足以顯示文本,問題在於ios7的標籤寬度錯誤。它無法重新調整標籤寬度。

有一點本事來解決這個問題。

UITextField *searchField = [searchBar valueForKey:@"_searchField"]; 


    UILabel *lable=[searchBar valueForKey:@"_placeholderLabel"]; 
    lable.font=[UIFont fontWithName:lable.font.fontName size:10.0]; 

根據您自己的搜索欄寬度計算字體大小。我也試圖改變特定標籤的寬度,但它永遠不會工作。

+0

的錯誤,我不知道如何計算的字體,因爲價值在我的情況的佔位符文本是可變的,有時它帶有1個字只,有時會把佔位符標籤和自動截斷,以點(...) – JAHelia

+0

@JAHelia檢查更新回答這個時候再 – CoolMonster

+0

以前更加完善謝謝爲了酷酷的黑客 – JAHelia