简单的iPad问题 - Safari浏览器 - 谷歌搜索栏

时间:2010-12-12 10:40:33

标签: iphone uitextfield

我正在尝试创建一个类似于iPad上safari浏览器的搜索栏。我认为它只是一个UITextview。 单击控件后,其大小将展开。当方向旋转时,它会相应地保持尺寸。 如何使用自动调整大小选项实现此目的?或者我是否需要手动编码来实现它?

2 个答案:

答案 0 :(得分:1)

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

搜索栏组件为您提供适当的功能。要使条形图正确调整大小,您只需将其固定到屏幕的适当侧面并使其可伸缩。尝试用IB打开this file作为示例。

答案 1 :(得分: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];

        }
    }


}