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

首頁 > 系統 > iOS > 正文

iOS 防鍵盤遮擋的實例

2019-10-21 18:42:12
字體:
來源:轉載
供稿:網友

當我們在UITextField輸入數據時經常彈出鍵盤遮擋界面,解決方法是:在彈出鍵盤時將整個UIVIew向上移動,在鍵盤消失時,UIVIew還原。

實例代碼如下:

@interface ViewController ()<UITextFieldDelegate>@property(nonatomic,strong)UITextField* tf;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.tf = [[UITextField alloc]initWithFrame:CGRectMake(10, 600, 100, 20)]; self.tf.delegate = self; self.tf.backgroundColor = [UIColor blackColor]; [self.view addSubview:self.tf];}#pragma mark life Circle-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; //后臺切換到前臺通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground)name:UIApplicationWillEnterForegroundNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self            selector:@selector(keyboardWillHide:)             name:UIKeyboardWillHideNotification            object:nil]; [self.view endEditing:YES];}- (void)applicationWillEnterForeground{ [self.view endEditing:YES];}-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self.view endEditing:YES];}#pragma mark UITextFieldDelegate-(void)textFieldDidBeginEditing:(UITextField *)textField{ //第一個cell不往上彈輸入框的位置 // if(indexPath.row!=0){ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil]; // }}-(void)textFieldDidEndEditing:(UITextField *)textField{}#pragma mark 鍵盤操作- (void)keyboardWillChange:(NSNotification *)note{ NSDictionary *userInfo = note.userInfo; CGFloat duration = [userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue]; CGRect keyFrame = [userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue]; //這個64是我減去的navigationbar加上狀態欄20的高度,可以看自己的實際情況決定是否減去; CGFloat moveY = keyFrame.origin.y -self.tf.frame.origin.y-self.tf.frame.size.height; NSLog(@"%f",moveY); [UIView animateWithDuration:duration animations:^{  self.view.transform = CGAffineTransformMakeTranslation(0, moveY); }];}- (void)keyboardWillHide:(NSNotification *)nsnotification{ [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil]; [UIView animateWithDuration:0.2 animations:^{  self.view.transform = CGAffineTransformMakeTranslation(0, 0); }];}@end

以上這篇iOS 防鍵盤遮擋的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜黄县| 永平县| 德令哈市| 周宁县| 万州区| 肇州县| 靖边县| 清流县| 九龙坡区| 泗阳县| 普洱| 抚顺市| 乐清市| 新宾| 平阴县| 武鸣县| 胶州市| 图木舒克市| 雷波县| 东台市| 延长县| 潍坊市| 资兴市| 泉州市| 高密市| 于都县| 体育| 黑龙江省| 渑池县| 太康县| 仪征市| 和林格尔县| 浦县| 天台县| 鸡东县| 潮州市| 外汇| 张家口市| 榆林市| 宣汉县| 漾濞|