简体   繁体   中英

Move the UIView up when the TextField is pressed

I have 2 text fields, If I click the text fields (In portrait mode) the keyboard is not hiding the textField. But when I change to the Landscape , Then the Keyboard overlays the textFields. I don't know how to set the frame size in the delegate methods (textFieldShouldBeginEditing,textFieldShouldEndEditing )so that it will work in both portrait and landscape. OR should i use the Scroll View ?? 在此处输入图片说明

在此处输入图片说明

You have to use a scrollView and make other UI elements as a subview of ScrollView . And frame size for scrollView should use a whole window size even in Landscape Mode too (For that you need to learn autoresizingmask ).. And then when you invoke the textField and keyboard appears ,you need to reduce the size our scrollView frame to scroll even your keyboard is on view..

below is my code for textFieldDidBeginEditing and textFieldDidEndEditing method:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{

    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: 0.2];
    scrollView.frame = CGRectMake(0,0,320,150); 
    [scrollView scrollRectToVisible:textField.frame animated:YES];
    [UIView commitAnimations];

}


- (void)textFieldDidEndEditing:(UITextField *)textField
{

    [self autoCalculateDownPayment];
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: 0.2];
    scrollView.frame = CGRectMake(0,0,320,416);
    [UIView commitAnimations];
}

I am not able to give you a 100% certain answer, both because it's a bit subjective and also because I am not able to actually see your app (print screens would be nice). But, in my opinion a UIScrollView would be a good answer. Another option is to animate your UITextField , so it can go "up" when the keyboard comes.

You should set your View Controller's frame up to show the editbox (for example) on center of the screen in (void)textFieldDidBeginEditing:(UITextField *)textField method. Calculations should depend on orientation. And set the frame to it's original in (void)textFieldDidEndEditing:(UITextField *)textField .

Also there are different approaches like using scrollview and changing it's content offset when editbox is touched.

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