iOS键盘自适应弹出效果

 更新时间:2017年06月30日 15:53:30   作者:弦外雨  
这篇文章主要为大家详细介绍了iOS键盘自适应弹出效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一、iOS键盘自适应弹出效果图

二、工程图

三、代码

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
<UITextFieldDelegate>


@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
  
  
  UITextField *field=[[UITextField alloc]initWithFrame:CGRectMake(100, 300, 50, 50)];
  field.backgroundColor=[UIColor redColor];
  field.delegate=self;
  [self.view addSubview:field];
  
  
  
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  
}
#pragma -mark -keyboard notificatin
- (void)keyboardWillShow:(NSNotification *)notification {
  NSDictionary *info = [notification userInfo];
  // keyboardHeight 为键盘高度
  CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
  [self animateViewWithKeyboardHeight:keyboardSize.height];
}

- (void)keyboardWillHide:(NSNotification *)notification {
  [self animateViewWithKeyboardHeight:0.0];
}

- (void)animateViewWithKeyboardHeight:(CGFloat)keyboardHeight {
  NSTimeInterval animationDuration = 0.3f;
  CGFloat height = self.view.bounds.size.height;
  CGFloat width = self.view.bounds.size.width;
  CGFloat topSize = 0.0;
  CGFloat viewH = self.view.frame.size.height-64;
  CGFloat deviceHeight = [UIScreen mainScreen].bounds.size.height;
  CGFloat animateH = deviceHeight - viewH - keyboardHeight;
  if (animateH >= 0) {
    topSize = 0;
    CGRect toRect = CGRectMake(0, topSize, width, height);
    self.view.frame = toRect;
    
  } else {
    topSize = animateH;
    CGRect toRect = CGRectMake(0, topSize, width, height);
    [UIView animateWithDuration:animationDuration animations:^{
      self.view.frame = toRect;
    }];
  }
}
#pragma -mark -UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
  [textField resignFirstResponder];
  
  return YES;
}

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


@end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • iOS左滑手势失效的解决方法

    iOS左滑手势失效的解决方法

    这篇文章主要为大家详细介绍了iOS左滑手势失效的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    这篇文章主要为大家详细介绍了iOS利用余弦函数实现卡片浏览工具,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • iOS runtime forwardInvocation详解及整理

    iOS runtime forwardInvocation详解及整理

    这篇文章主要介绍了 iOS runtime forwardInvocation详解及整理的相关资料,需要的朋友可以参考下
    2017-02-02
  • iOS中tableView cell分割线的一些设置技巧

    iOS中tableView cell分割线的一些设置技巧

    在项目开发中我们会常常遇到tableView 的cell分割线显示不全,左边会空出一截像素,更有甚者想改变系统的分割线,下面通过这篇文章来一起学习学习在iOS中tableView cell分割线的一些设置技巧,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-05-05
  • iOS整个APP实现灰色主题的示例代码

    iOS整个APP实现灰色主题的示例代码

    这篇文章主要介绍了iOS整个APP实现灰色主题的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • iOS简单抽屉效果的实现方法

    iOS简单抽屉效果的实现方法

    这篇文章主要为大家详细介绍了iOS简单抽屉效果的实现方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐

    iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐

    这篇文章主要介绍了iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐的相关资料,需要的朋友可以参考下
    2015-09-09
  • iOS中捕获日志与异常示例详解

    iOS中捕获日志与异常示例详解

    在日常的工作中,日志是不可缺少的一个环节,下面这篇文章主要给大家介绍了iOS中捕获日志与异常的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-04-04
  • iOS获取设备唯一标识的实现步骤

    iOS获取设备唯一标识的实现步骤

    这篇文章主要介绍了iOS获取设备唯一标识的实现步骤,帮助大家更好的理解和学习ios开发,感兴趣的朋友可以了解下
    2021-04-04
  • iOS开发中使用UIScrollView实现无限循环的图片浏览器

    iOS开发中使用UIScrollView实现无限循环的图片浏览器

    这篇文章主要介绍了iOS开发中使用UIScrollView实现无限循环的图片浏览器的方法,感兴趣的小伙伴们可以参考一下
    2016-03-03

最新评论