当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS中只让textField使用键盘通知的实例代码

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

光域网文件下载,虎跃快客查询,吉尔尼斯战袍在哪买

代码:

#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使用键盘通知的实例代码,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网