【问题标题】:How to scroll the textfields when view is moved up?视图向上移动时如何滚动文本字段?
【发布时间】:2015-10-26 21:00:05
【问题描述】:

我在注册屏幕上有文本字段,所以当键盘出现时我向上移动视图。现在我想在视图向上移动时滚动文本字段。

向上移动视图的代码:

         CGRect rect = self.view.frame;
         rect.origin.y += 220;
         rect.size.height -= 220;
         self.view.frame = rect;

这是雇佣兵

我在顶部有UIScrollView,然后在UIScrollView 下我有UIView 我称之为contentView。在contentView 上我添加了一些文本字段。所以我的父视图向上移动了,但我当我查看时,我无法上下滚动文本字段。我为滚动设置了以下值。

    self.cvHeight.constant=500;
    self.scrollView.contentSize=CGSizeMake(500, 500);

这里cvHeightcontentView 的高度约束出口,scrollViewUIScrollView 的出口

编辑: 当我没有向上移动 self.view 的父视图时,我可以滚动浏览,但是如果我向上移动父视图,那么我的文本字段不会向下滚动,它们只是向上移动。

【问题讨论】:

    标签: ios objective-c iphone ipad uiscrollview


    【解决方案1】:

    当您使用 UIScrollView 时,这将是最适合您的。使用TPKeyboardAvoidingScrollView

    如何使用? 很简单

    要与 UITableViewController 类一起使用,请将 TPKeyboardAvoidingTableView.m 和 TPKeyboardAvoidingTableView.h 放入您的项目中,并将您的 UITableView 设置为 xib 中的 TPKeyboardAvoidingTableView。如果您没有在控制器中使用 xib,我知道没有简单的方法使其 UITableView 成为自定义类:阻力最小的路径是为其创建 xib。

    对于非 UITableViewControllers,将 TPKeyboardAvoidingScrollView.m 和 TPKeyboardAvoidingScrollView.h 源文件拖放到项目中,将 UIScrollView 弹出到视图控制器的 xib 中,将滚动视图的类设置为 TPKeyboardAvoidingScrollView,然后将所有控件放在该滚动视图中。您也可以通过编程方式创建它,而无需使用 xib - 只需使用 TPKeyboardAvoidingScrollView 作为您的顶级视图。

    【讨论】:

      【解决方案2】:
      - (void)keyboardWillShow:(NSNotification*)notification
      {
          CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
          UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0,0.0,keyboardSize.height, 0.0);
      
          self.scrollViewPost.contentInset = contentInsets;
          self.scrollViewPost.scrollIndicatorInsets = contentInsets;
      
          CGRect rect = self.scrollViewPost.frame;
          rect.size.height = keyboardSize.height+_scrollViewPost.frame.size.height+84;
      
      
          if (!CGRectContainsPoint(rect, self.txtPost.frame.origin))
          {
              CGPoint scrollPoint = CGPointMake(0.0, 264+Table_Y);
              [self.scrollViewPost setContentOffset:scrollPoint animated:NO];
              self.viewPutPost.frame=CGRectMake(0, _viewPutPost.frame.origin.y-254, 320, 30);
      
      
          }
      }
      
      - (void)keyboardWillHide:(NSNotification *)notification
      {
          UIEdgeInsets contentInsets = UIEdgeInsetsZero;
          self.scrollViewPost.contentInset = contentInsets;
          self.scrollViewPost.scrollIndicatorInsets = contentInsets;
      
          self.viewPutPost.frame=CGRectMake(0,537 , 320, 30);
      
      }
      

      【讨论】:

        【解决方案3】:

        ViewController.h

        #import <UIKit/UIKit.h>
        @interface SignUp : UIViewController <UIScrollViewDelegate>
        {
        
        }
        @property (nonatomic, assign) UITextField *activeTextField;
        

        ViewController.m

        @implementation SignUp
        @synthesize activeTextField;
        
        - (void)viewDidLoad
        {
        
        
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
        
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        
            [super viewDidLoad];
            // Do any additional setup after loading the view from its nib.
        }
        
        #pragma mark - Textfield
        
        - (void)keyboardWasShown:(NSNotification *)notification
        {
        
            // Step 1: Get the size of the keyboard.
            CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
        
        
            // Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
            UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
        
            _scrl.contentInset = contentInsets;
            _scrl.scrollIndicatorInsets = contentInsets;
        
        
            // Step 3: Scroll the target text field into view.
            CGRect aRect = self.view.frame;
            aRect.size.height -= keyboardSize.height;
            if (!CGRectContainsPoint(aRect, self.activeTextField.frame.origin) )
            {
                CGPoint scrollPoint = CGPointMake(0.0, self.activeTextField.frame.origin.y - (keyboardSize.height));
                [_scrl setContentOffset:scrollPoint animated:YES];
            }
        }
        - (void) keyboardWillHide:(NSNotification *)notification {
        
            UIEdgeInsets contentInsets = UIEdgeInsetsZero;
            _scrl.contentInset = contentInsets;
            _scrl.scrollIndicatorInsets = contentInsets;
        }
        - (void)textFieldDidEndEditing:(UITextField *)textField
        {
            self.activeTextField = nil;
        }
        
        - (void)textFieldDidBeginEditing:(UITextField *)textField
        {
            self.activeTextField = textField;
        
        
        }
        
        - (BOOL)textFieldShouldReturn:(UITextField *)textField;              // called when 'return' key pressed. return NO to ignore.
        {
            if(textField == _txt_name)
            {
                [_txt_email becomeFirstResponder];
                return NO;
            }
            else if(textField == _txt_email)
            {
                [_txt_pwd becomeFirstResponder];
                return NO;
            }
            else if(textField == _txt_pwd)
            {
                [_txt_repwd becomeFirstResponder];
                return YES;
            }
            else if (textField == _txt_repwd)
            {
                [textField resignFirstResponder];
                return YES;
            }
            else
            {
                return YES;
            }
        }
        

        【讨论】:

          【解决方案4】:

          您不必向上移动整个视图。

          相反,根据需要更改滚动视图的 contentOffset 属性,并在键盘启动时相应地增加 scrollView.contentSize。 当键盘向下时,将 scrollView.contentOffset 设置为 CGPointZero 和 scrollView.contentSize

          键盘启动时:

          CGFloat yOffset = ;             //Set According to the textField
          self.scrollView.contentOffset = CGPointMake(0,-yOffset);
          
          //Increase content size to enable scrolling till the end.
          CGFloat contentHeight =  self.crollView.contentSize.height + keyboardHeight;
          
          self.scrollView.contentSize = CGSizeMake(kScreenWidth, contentHeight);     
          

          当键盘关闭时:

          self.scrollView.contentOffset = CGPointMake(0,0);
          
          //Increase content size to enable scrolling till the end.
          CGFloat contentHeight =  self.crollView.contentSize.height - keyboardHeight;
          
          self.crollView.contentSize = CGSizeMake(kScreenWidth, contentHeight);     
          

          增加内容大小将使您能够在键盘向上时滚动滚动视图直到底部。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-12-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-06-22
            • 1970-01-01
            相关资源
            最近更新 更多