IOS开发中键盘如何在自适中弹出效果
来源:爱站网时间:2020-11-18编辑:网友分享
这篇文章主要为大家详细介绍了iOS键盘自适应弹出效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
使用uitextfield或uitextview输入框时,需要弹出虚拟键盘进行输入,而弹出虚拟键盘时,输入框不会移动,今天就由爱站技术频道小编给大家介绍一下炒股要怎样选择购买的相关内容吧。
一、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开发中键盘如何在自适中弹出效果,相关的资讯我们也介绍了很多,想要了解更多的朋友还可以收藏js.aizhan.com哦。
上一篇:IOS开发超强中文分词的实现方法
下一篇:关于UITableView的详解