出现和消失键盘时,Objective-C Scroll View无法正常工作 [英] Objective-C Scroll VIew not working when keyboard appears and dissapears

查看:154
本文介绍了出现和消失键盘时,Objective-C Scroll View无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我希望我的backgroundLoginView在键盘出现时向上移动. 意见流就像 superView-> scrollView-> backgroundView-> backgroundLoginView-> textFeild1(登录ID)和textFeild2(密码) 这是我想要的代码,当键盘显示密码时,backgroundLoginView自动向上移动. 我的背景登录视图

Hello I want my backgroundLoginView to move up when keyboard appears. the flow of views are like superView->scrollView->backgroundView->backgroundLoginView->textFeild1(Login Id) and textFeild2(Password) Here are my codes I want when my keyboards appears for Password the backgroundLoginView automatically moves up. MY backgroundLoginView

问题

//LoginViewController.h
@interface LoginViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIView *backgroundLoginView;
@property (strong, nonatomic) IBOutlet UIView *backgroundView;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;

@end 
//LoginViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]
                                           initWithTarget:self
                                           action:@selector(hideKeyBoard)];
    [_backgroundView addGestureRecognizer:tapGesture];

    _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width, CGRectGetMaxY(_backgroundView.frame));

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
- (void)keyboardWillShow:(NSNotification*)aNotification
{

    CGSize kbSize = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    NSTimeInterval duration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    [UIView animateWithDuration:duration animations:^{
        UIEdgeInsets edgeInsets = UIEdgeInsetsMake(0, 0, kbSize.height, 0);
        [_scrollView setContentInset:edgeInsets];
        [_scrollView setScrollIndicatorInsets:edgeInsets];
    }];
}

- (void)keyboardWillHide:(NSNotification*)aNotification
{

    NSTimeInterval duration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    [UIView animateWithDuration:duration animations:^{
        UIEdgeInsets edgeInsets = UIEdgeInsetsZero;
        [_scrollView setContentInset:edgeInsets];
        [_scrollView setScrollIndicatorInsets:edgeInsets];
    }];
}

推荐答案

您可以尝试以下方法:

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

  UITableViewCell *cell = (UITableViewCell *)[textField superview]; 
  NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
  [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
  return YES;

}

这篇关于出现和消失键盘时,Objective-C Scroll View无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆