欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS 防鍵盤遮擋的實例

 更新時間:2018年01月15日 09:40:41   作者:鍵盤舞者113  
下面小編就為大家分享一篇iOS 防鍵盤遮擋的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

當我們在UITextField輸入數(shù)據(jù)時經(jīng)常彈出鍵盤遮擋界面,解決方法是:在彈出鍵盤時將整個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加上狀態(tài)欄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 防鍵盤遮擋的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論