开发者

Simple iPad question - Safari browser - Google search bar

I'm trying to create a sear开发者_开发知识库ch bar similar to the one in safari browser on iPad. I think its a UITextview only. On click of the control, its size will expand. And when orientation is rotated, it maintains the size accordingly. How can I achieve that using auto-resizing option? Or do I've to code manually to achieve it?


You can do all this directly in Interface Builder.

The Search Bar component gives you the appropriate functionality. To make the bar resize properly, you just need to anchor it to the appropriate sides of the screen and make it stretchable. Try opening this file with IB for an example.


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

        }
    }


}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜