【问题标题】:Implementing scroll View in ios for a particular case针对特定情况在 ios 中实现滚动视图
【发布时间】:2015-04-13 21:45:53
【问题描述】:

在我的应用程序中,我有 8 个文本字段。当键盘出现时,我已经实现了滚动视图,但是当我开始在第一个文本字段中输入文本时,视图会滚动并隐藏我当前的焦点文本字段。我希望滚动应该当我在某个特定的文本字段中输入文本时发生,假设从 5 日开始。我怎么能这样做。

【问题讨论】:

  • 对您的问题有许多高质量的回答。鉴于“特殊情况”非常笼统,编辑您的标题可能有意义。

标签: ios objective-c uiscrollview


【解决方案1】:

使用 NSNotifications 知道键盘的高度:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboardEditProfile) name:UIKeyboardWillShowNotification object:nil];

然后 实现这个函数来获取键盘的高度:

-(void)showKeyBoard:(NSNotification *)notification
{

NSDictionary *info=[notification userInfo];
 keyboardSize=[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

}

这将为您提供键盘的大小。

现在使用按其出现的递增顺序设置文本字段的标签,并将您的视图控制器设置为它们的委托。

然后使用这个委托方法来设置scrollView的contentOffset:

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

【讨论】:

    【解决方案2】:

    您好,我已经使用该代码很长时间了,它对于滚动视图和表格视图非常有用

    #pragma mark
    #pragma mark - text field delegate method
    -(void)textFieldDidBeginEditing:(UITextField *)textField
    {
        [self showKeyBoard:YES];
        [self setTableOffsetForTextField:textField];
    }
    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [textField resignFirstResponder];
        [self.view endEditing:YES];
        [self showKeyBoard:NO];
        return YES;
    }
    #pragma mark - Show Hide Key Board
    - (void)showKeyBoard:(BOOL)boolValue
    {
        scrollView.contentInset = UIEdgeInsetsMake(0, 0, boolValue ? ([self kbHeight1]) : 0.0, 0.0);
    }
    
    
    - (CGFloat)kbHeight1
    {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    
            UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    
            if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
                return 264.0;
            }
            else
            {
                return 352.0;
            }
        }
        else
        {
            return 216.0;
        }
    
    }
    - (void)setTableOffsetForTextField:(UIView *)textField
    {
    
        UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)){
    
            return;
        }
    
    
        CGPoint point = [textField convertPoint:textField.frame.origin toView:scrollView];
        CGFloat diff = [self difference];
        CGFloat pos = (point.y - diff);
        if (pos < 0 || pos > scrollView.contentSize.height - diff) {
            pos = 0;
        }
        [scrollView setContentOffset:CGPointMake(0, pos) animated:YES];
    }
    
    
    -(CGFloat)difference{
        CGSize screenSize = self.view.frame.size;
    
        CGFloat diff;
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    
            diff = (screenSize.height > 768) ? 264.0 : 250;
    
        }
        else
        {
            diff = (screenSize.height == 480) ? 120.0 : 70.0;        
        }
        return diff;
    }
    

    希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      UITextFieldDelegate的实现方法:

      - (void)textFieldDidBeginEditing:(UITextField *)textField {
          [self.scroolView scrollRectToVisible:textfield.frame animated:YES]; 
      }
      

      您可以在此方法中实现更多自定义逻辑来计算 visibleFrame。

      【讨论】:

        【解决方案4】:

        你可以使用标签来做到这一点。

        为每个文本字段添加 1-8 的标签,对应 8 个文本字段。检查文本字段的标记值是否等于或大于 5,然后仅应用您的滚动视图,否则返回;

        - (void)textFieldDidBeginEditing:(UITextField *)textField {
            if(textField.tag==5 || textField.tag>=5)
               {
               //Implement scrollview here
               }
        }
        

        希望对您有所帮助...!

        【讨论】:

          猜你喜欢
          • 2020-05-31
          • 1970-01-01
          • 2014-02-24
          • 1970-01-01
          • 2013-08-03
          • 1970-01-01
          • 1970-01-01
          • 2011-08-18
          • 2016-02-18
          相关资源
          最近更新 更多