【代码笔记】iOS-键盘自适应弹出

发布时间:2017-7-9 7:33:30编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"【代码笔记】iOS-键盘自适应弹出 ",主要涉及到【代码笔记】iOS-键盘自适应弹出 方面的内容,对于【代码笔记】iOS-键盘自适应弹出 感兴趣的同学可以参考一下。

#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.}


上一篇:[BZOJ2820]YY的GCD

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款