2010-12-12 87 views
0

我試圖創建一個類似於在iPad上Safari瀏覽器中的搜索欄。我認爲它只是一個UITextview。 單擊控件時,其大小將擴大。當方向旋轉時,它會相應地保持尺寸。 如何使用自動調整大小選項來實現該功能?還是我必須手動編寫代碼才能實現它?簡單的iPad問題 - Safari瀏覽器 - 谷歌搜索欄

回答

1

您可以直接在Interface Builder中完成所有操作。

搜索欄組件爲您提供了相應的功能。要使條形圖正確調整大小,只需將其錨定到屏幕的適當邊,並使其具有可伸縮性。例如,嘗試用IB打開this file

0
Use the below code to your controller and make sure the you have a textfield delegate. 

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    UIInterfaceOrientation orientation = self.interfaceOrientation; 
    if (orientation== UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { 

     if(textField==searchField){ 


      CGRect searchFrame = searchField.frame; 
      searchFrame.size.width += 150; 
      searchFrame.origin.x -= 150; 


      [UIView beginAnimations: @"GrowTextField" context: nil]; 
      { 
       searchField.frame = searchFrame; 
       [UIView setAnimationDuration: 0.5]; 
      } 
      [UIView commitAnimations]; 
     } 


    } 

    else if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) 
    {  
     if(textField==searchField){ 


      CGRect searchFrame = searchField.frame; 
      searchFrame.size.width += 150; 
      searchFrame.origin.x -= 150; 

      [UIView beginAnimations: @"GrowTextField" context: nil]; 
      { 
       searchField.frame = searchFrame; 
       [UIView setAnimationDuration: 0.5]; 
      } 
      [UIView commitAnimations]; 
     } 
    } 

} 



- (void)textFieldDidEndEditing:(UITextField *)textField{ 
    UIInterfaceOrientation orientation = self.interfaceOrientation; 
    if (orientation== UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { 
     if(textField==searchField){ 

      CGRect searchFrame = searchField.frame; 
      searchFrame.size.width -= 150; 
      searchFrame.origin.x += 150; 


      [UIView beginAnimations: @"ShrinkTextField" context: nil]; 
      { 
       searchField.frame = searchFrame; 
       [UIView setAnimationDuration: 0.5]; 
      } 
      [UIView commitAnimations]; 

     } 


    } 

    else if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) 
    {  

     if(textField==searchField){ 

      CGRect searchFrame = searchField.frame; 
      searchFrame.size.width -= 150; 
      searchFrame.origin.x += 150; 


      [UIView beginAnimations: @"ShrinkTextField" context: nil]; 
      { 
       searchField.frame = searchFrame; 
       [UIView setAnimationDuration: 0.5]; 
      } 
      [UIView commitAnimations]; 

     } 
    } 


}