国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

iOS鍵盤自適應(yīng)彈出效果

2020-07-26 02:46:24
字體:
供稿:網(wǎng)友

一、iOS鍵盤自適應(yīng)彈出效果圖

二、工程圖

三、代碼

ViewController.h

#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UITextFieldDelegate>@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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 锦屏县| 灌阳县| 珲春市| 青州市| 科尔| 渝北区| 大悟县| 云和县| 海盐县| 景洪市| 平陆县| 耿马| 上高县| 太仆寺旗| 双江| 武隆县| 湘阴县| 杨浦区| 密山市| 游戏| 常熟市| 隆安县| 巴彦县| 庄浪县| 扬州市| 新野县| 元氏县| 杨浦区| 西乌珠穆沁旗| 涪陵区| 阳朔县| 嵩明县| 息烽县| 平邑县| 克拉玛依市| 柞水县| 三都| 琼结县| 通海县| 聂拉木县| 德保县|