网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

iOS如何实现键盘自适应弹出效果

这篇文章主要介绍了iOS如何实现键盘自适应弹出效果,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、微信小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了梅县免费建站欢迎大家使用!

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

iOS如何实现键盘自适应弹出效果

二、工程图

iOS如何实现键盘自适应弹出效果

三、代码

ViewController.h

#import 

@interface ViewController : UIViewController



@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如何实现键盘自适应弹出效果
网页地址:http://bjjierui.cn/article/pcecjh.html

其他资讯