简体   繁体   中英

How to set UITextfield position during device orientation

I have one image view on that image view i put two textField there. When i put device in portrait mode position on UITextfield is correct. Once i rotate my device position of UITextfiled change. Please help me.I am using IOS 6, Thanks in Advance.

There are many ways to implement it : 1. Constraints ( adjusting from xcode->editor->pin) 2. By pragmatically.

I personally suggest you to take a deep knowledge of this

by Ray Wenderlich

Try to implement -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration . This method gets called when the device is rotated. Here you can set the frame of the UITextField .

Alternatively you can try to set the AutoresizingMask.

  1. You can achieve this by AUTO LAYOUT (iOS6 new feature). Any new xib that you create in iOS6 will have Auto Layout activated by default. You have to adjust constraint. for this select your text field and image view. Now From Xcode's Editor menu, select Pin. Pin your text field and image view.

for more about auto layout please read this awesome tutorial. http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2

  1. You can also achieve this by pragmatically.

set frames for your text field and image in willAnimateRotationToInterfaceOrientation

   - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                     duration:(NSTimeInterval)duration
   {
        [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];

        if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft
        ||  toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
        {
            // set frame for your text field and image in landscape orientation.
        }
        else
        {
            // set frame for your text field and image in landscape orientation.
        }
   }

Thats It.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM