iOS中只让textField使用键盘通知的实例代码

 更新时间:2017年07月19日 11:23:14   作者:弦外雨  
本文通过实例代码给大家介绍了OS中只让textField使用键盘通知的操作方法,代码简单易懂,非常不错,具有参考借鉴加载,需要的的朋友参考下吧

代码:

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
  //为textField增加键盘事件
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addKeyboardNoti) name:UITextFieldTextDidBeginEditingNotification object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeKeyboardNoti) name:UITextFieldTextDidEndEditingNotification 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 -UITextFieldText Notification
//增加键盘事件
-(void)addKeyboardNoti
{
  NSLog(@"------addKeyboardNoti-------");
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
//移除键盘事件
-(void)removeKeyboardNoti
{
  NSLog(@"------removeKeyboardNoti---------");
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}
@end

总结

以上所述是小编给大家介绍的iOS中只让textField使用键盘通知的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • iOS实现列表折叠效果

    iOS实现列表折叠效果

    这篇文章主要为大家详细介绍了iOS实现列表折叠效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • iOS创建对象的不同姿势详解

    iOS创建对象的不同姿势详解

    这篇文章主要介绍了iOS创建对象的不同姿势,文中介绍的很详细,对大家具有一定的参考价值,有需要的朋友们下面来一起学习学习吧。
    2017-02-02
  • iOS密码在进入后台1小时后重新设置

    iOS密码在进入后台1小时后重新设置

    这篇文章主要介绍了iOS密码在进入后台1小时后重新设置的相关资料,需要的朋友可以参考下
    2017-08-08
  • ios uicollectionview实现横向滚动

    ios uicollectionview实现横向滚动

    这篇文章主要为大家详细介绍了ios uicollectionview实现横向滚动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-03-03
  • IOS 开发之Object-C中的对象详解

    IOS 开发之Object-C中的对象详解

    这篇文章主要介绍了IOS 开发之Object-C中的对象详解的相关资料,需要的朋友可以参考下
    2017-06-06
  • iOS获取图片区域主色的方法

    iOS获取图片区域主色的方法

    这篇文章主要为大家详细介绍了iOS获取图片区域主色的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-06-06
  • 举例讲解iOS应用开发中对设计模式中的策略模式的使用

    举例讲解iOS应用开发中对设计模式中的策略模式的使用

    这篇文章主要介绍了iOS应用设计中对设计模式中的策略模式的使用,示例代码为传统的Objective-C语言,需要的朋友可以参考下
    2016-03-03
  • iOS实现毫秒倒计时的方法详解

    iOS实现毫秒倒计时的方法详解

    倒计时在我们日常开发中必不可少,最近在公司的一个项目中就遇到了这个需求,本文着重介绍的是利用iOS实现毫秒倒计时的方法,文中给出了详细的示例代码,需要的朋友可以参考借鉴,下面来一起学习学习吧。
    2017-04-04
  • iOS UIWebView 通过 cookie 完成自动登录实例

    iOS UIWebView 通过 cookie 完成自动登录实例

    本篇文章主要介绍了iOS UIWebView 通过 cookie 完成自动登录实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-04-04
  • iOS 修改alertViewController弹框的字体颜色及字体的方法

    iOS 修改alertViewController弹框的字体颜色及字体的方法

    下面小编就为大家分享一篇iOS 修改alertViewController弹框的字体颜色及字体的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01

最新评论