键盘弹出高度和遮挡问题

来源:互联网 发布:竞价排名优化方法 编辑:程序博客网 时间:2024/04/30 06:03

#import "LoginViewController.h"

#import "ClssNameViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>

@property (strong,nonatomic) IBOutletUIButton *buttonLogin;

@property (strong,nonatomic) IBOutletUITextField *textName;

@property (strong,nonatomic) IBOutletUITextField *textPwd;

@property (strong,nonatomic) IBOutletUITextField *textSelect;


@end


@implementation LoginViewController

-(void)viewDidAppear:(BOOL)animated

{

    [superviewDidAppear:animated];

    [[NSNotificationCenterdefaultCenter] addObserver:self

                                            selector:@selector(keyboardDidShowOrHide:)

                                                name:UIKeyboardWillShowNotification

                                              object:nil];

    [[NSNotificationCenterdefaultCenter] addObserver:self

                                            selector:@selector(keyboardDidShowOrHide:)

                                                name:UIKeyboardWillHideNotification

                                              object:nil];

}

-(void)viewWillDisappear:(BOOL)animated

{

    [superviewWillDisappear:animated];

     [[NSNotificationCenterdefaultCenter] removeObserver:self];

    

}

#pragma mark - Keyboard methods

/*

 view将要上升的高度 键盘高度-(屏幕高度 文本框在view上的y坐标);

 */

-(void)keyboardDidShowOrHide:(NSNotification *)notification

{

   NSDictionary *userInfo = [notification userInfo];

   NSTimeInterval animationDuration;

   UIViewAnimationCurve animationCurve;

   CGRect keyboardEndFrame;//键盘的frame

    

    [[userInfoobjectForKey:UIKeyboardAnimationCurveUserInfoKey]getValue:&animationCurve];

    [[userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey]getValue:&animationDuration];

    [[userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey]getValue:&keyboardEndFrame];

    

    [UIViewbeginAnimations:nilcontext:nil];

    [UIViewsetAnimationDuration:animationDuration];

    [UIViewsetAnimationCurve:animationCurve];

    

   CGRect newFrame = self.view.frame;//viewframe

   

    

   if (self.textSelect ==nil) {

        newFrame.origin.y =0.0;

    }

   else

    {

        newFrame.origin.y = keyboardEndFrame.origin.y - newFrame.size.height;

    }

   self.view.frame = newFrame;

    

    [UIViewcommitAnimations];

}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

   self.textSelect = textField;

    return YES;

}

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

   self.textPwd.delegate =self;

   self.textName.delegate =self;

   UIImageView* imageViewTap = (UIImageView* )[self.viewviewWithTag:1];

    imageViewTap.userInteractionEnabled =YES;

    UITapGestureRecognizer* tap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tapConcelText:)];

    [imageViewTapaddGestureRecognizer:tap];

}

-(void)tapConcelText:(UITapGestureRecognizer*)tap

{

    self.textSelect =nil;

    [self.textPwdresignFirstResponder];

    [self.textNameresignFirstResponder];

 

}


@end

0 0
原创粉丝点击