2010-12-12 3 views
0

iPadのSafariブラウザに類似した検索バーを作成しようとしています。私はそれがUITextviewだけだと思う​​。 コントロールをクリックすると、そのサイズが拡大されます。向きが回転すると、それに応じてサイズが維持されます。 自動サイズ変更オプションを使用して、どうすれば実現できますか?または、それを達成するために手動でコードを作成する必要がありますか?iPadの簡単な質問 - Safariブラウザ - Google検索バー

答えて

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

     } 
    } 


}