键盘遮挡问题

作者: 徐老茂 | 来源:发表于2015-12-08 17:02 被阅读406次

不知道大家是否遇见过这种问题,就是在准备输入文字的时候键盘弹出来遮挡住了输入框,导致无法输入.今天给大家讲讲这个问题.看看解决之后的效果图吧.


当键盘弹出,整个界面上移,但幅度不会太大,位置会刚刚好.看代码吧.

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate>
@property(nonatomic, strong)UITextField *textField;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
    imageView.image =[UIImage imageNamed:@"5.jpg"];
    [self.view addSubview:imageView];
    
    self.textField = [[UITextField alloc]initWithFrame:CGRectMake(65, 500, 250, 50)];
    _textField.borderStyle = 3;
    _textField.delegate = self;
    [self.view addSubview:_textField];
    
    //监听,分别表示当键盘显示和隐藏时执行的方法
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
    
}
#pragma mark 键盘出现时触发的方法
- (void)keyboardWillShow:(NSNotification *)note
{
    //得到键盘的高度
    NSDictionary *info = [note userInfo];
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    //背景图向上移动键盘的高度,加号后面那句话是为了不让textfiled里键盘太远
    CGFloat newY = -keyboardSize.height + (self.view.frame.size.height - (_textField.frame.origin.y + _textField.frame.size.height + 40));
    
    //判断键盘出来后是否会遮住textfiled
    if (keyboardSize.height > self.view.frame.size.height - (_textField.frame.origin.y + _textField.frame.size.height + 40)) {
        //动画效果
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:.3];
        
        self.view.frame = CGRectMake(0, newY, self.view.frame.size.width, self.view.frame.size.height);
        //动画开始
        [UIView commitAnimations];
    }
}

#pragma mark 键盘隐藏时触发的方法
-(void)keyboardWillHide
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.3];
    //回到最初的位置
    self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [UIView commitAnimations];
}

#pragma mark 回收键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_textField resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //点击键盘上的return按键回收键盘(失去第一响应)
    [textField resignFirstResponder];
    return YES;
}

-(void)viewDidDisappear:(BOOL)animated
{
    //当界面跳转时移除键盘的监听
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];//移除监听
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

很简单吧,大家试试吧.好了,今天就到这里,祝大家开心😄

相关文章

网友评论

  • 别闹晓敏:有个问题,当点击tabbar回到这个界面的时候,方法就失去作用了
    徐老茂:@别闹晓敏 你是说位置改变太多吗?
    别闹晓敏: @徐老茂 我发错了,这个我知道,我用tabbar 会改变tabbar的好多
    徐老茂:@别闹晓敏 最后一行[[NSNotificationCenter defaultCenter] removeObserver:self];//移除监听,你把这句话删了试一试

本文标题:键盘遮挡问题

本文链接:https://www.haomeiwen.com/subject/psmehttx.html