简体   繁体   English

选择UITextField时将UIScrollView向上移动

[英]move UIScrollView up when UITextField is selected

subview assigned to the UIScrollView. 分配给UIScrollView的子视图。 when the UITextField is selected UIScrollView should move up. 当选择UITextField时,UIScrollView应该向上移动。 I tried with below code but its not working. 我尝试使用下面的代码,但无法正常工作。

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

    if( textField == citytextfield)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.4];
        //set Y according to keyBoard height
        [_scrolling setFrame:CGRectMake(0.0,-220.0,320.0,460.0)];

        [UIView commitAnimations];
    }



}

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

    [textField resignFirstResponder];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [_scrolling setFrame:CGRectMake(0.0,0.0,320.0,460.0)];
    [UIView commitAnimations];
}

使用UIScrollView setContentOffset方法使其向上滚动,您需要将值50更改为所需的值。

[_scrolling setContentOffset:CGPointMake(0,50) animated:YES];

download the tpkeyboardavoidingscrollview. 下载tpkeyboardavoidingscrollview。
give your scrollview's class name to tpkeyboardavoidingscrollview. 将您的scrollview的类名指定为tpkeyboardavoidingscrollview。

在此处输入图片说明

  @property (weak, nonatomic) IBOutlet TPKeyboardAvoidingScrollView *scrollView;

set the property and give the connection to scrollview. 设置属性并将连接设置为scrollview。
In your viewcontroller.h 在您的viewcontroller.h中

#import "TPKeyboardAvoidingScrollView"

then check it. 然后检查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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