当显示键盘时,自定义UIView正在推送部分内容

时间:2018-03-08 18:51:35

标签: ios objective-c uiview uiviewcontroller uikeyboard

我已经构建了一个自定义的UIView,我将其添加到UIViewController。

enter image description here

我希望我的自定义UIView向上移动以显示键盘隐藏的任何文本字段。但是,它似乎只是推动了UIView的其他元素。

我做错了什么? 没有键盘:

enter image description here

注意当键盘显示时,日期和公司名称如何被推到UITextFields后面

enter image description here

LogInViewController.m

@interface LogInViewController () <UITextFieldDelegate> {
    LoginView * loginView;
}
@property (nonatomic, assign) CGRect   keyboardFrame;
@end

- (void)viewDidLoad {
    [super viewDidLoad];

    loginView = [[LoginView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:loginView];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [nc addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)handleKeyboardDidShow:(NSNotification *)notification {
    NSTimeInterval animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];

    _keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    _keyboardFrame = [self.view convertRect:_keyboardFrame fromView:self.view.window];

    [UIView animateWithDuration:animationDuration animations:^{
        CGRect frm = loginView.frame;
        frm.size.height = CGRectGetMinY(_keyboardFrame);
        loginView.frame = frm;
    }];
}

- (void)handleKeyboardWillHide:(NSNotification *)notification {
    NSTimeInterval animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];

    [UIView animateWithDuration:animationDuration animations:^{
        CGRect frm = loginView.frame;
        frm.size.height = CGRectGetMaxY(self.view.bounds);
        loginView.frame = frm;
    }];
}

LoginView.h

@interface LoginView : UIView
@property (strong, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UITextField *txtUsername;
@property (weak, nonatomic) IBOutlet UITextField *txtPassword;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *indicator;
@property (weak, nonatomic) IBOutlet UIButton *btnSignin;
@property (weak, nonatomic) IBOutlet UILabel *lblVersion;
@property (weak, nonatomic) IBOutlet UILabel *lblMessageArea;
@end

LoginView.m

@implementation LoginView
- (instancetype) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self){

        [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
        [self addSubview:self.contentView];
        self.contentView.frame = self.bounds;
    }
    return self;
}
@end

1 个答案:

答案 0 :(得分:0)

你覆盖了_keboardFrame,所以第一个作业没用。

    _keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    _keyboardFrame = [self.view convertRect:_keyboardFrame fromView:self.view.window];

我建议使用UIKeyboardWillShowNotification来获得更加一致的动画:

// for your code, I prefer case 1, to shift up the entire view. Anyway, if you want, you can use the second option that I've commented. But the best option is to use constraints, then update that constraint
- (void)keyboardWillShow:(NSNotification *)notification {
    NSTimeInterval animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];

    CGFloat keyboardHeight = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;

    [UIView animateWithDuration:animationDuration animations:^{
        CGRect frm = loginView.frame;
        // 1. move up the view
        frm.origin.y = -keyboardHeight;
        // 2. change the height
        //frm.size.height = self.view.bounds.size.height - keyboardHeight;
        loginView.frame = frm;
    }];
}

- (void)keyboardWillHide:(NSNotification *)notification {
    NSTimeInterval animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
        [UIView animateWithDuration:animationDuration animations:^{
            loginView.frame = self.view.bounds;
        }];
}

这对你好吗?