国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 茂名市| 昔阳县| 涞水县| 丰城市| 镇原县| 海丰县| 余江县| 曲松县| 岳西县| 华亭县| 于都县| 增城市| 阳西县| 南皮县| 沐川县| 门源| 五常市| 郑州市| 花莲县| 江城| 陕西省| 寿宁县| 阿拉尔市| 栾城县| 玉环县| 忻城县| 云阳县| 类乌齐县| 东丽区| 沙田区| 耒阳市| 府谷县| 内丘县| 灯塔市| 龙里县| 万山特区| 武定县| 邵阳市| 天祝| 嘉峪关市| 乌恰县|