簡體   English   中英

顯示鍵盤時,自定義UIView將部分內容向上推

[英]Custom UIView is Pushing Partial Contents Up When the Keyboard is Shown

我建立了一個自定義UIView,將其添加到UIViewController中。

在此處輸入圖片說明

我希望自定義UIView向上移動以顯示鍵盤隱藏的所有文本字段。 但是,它似乎只會推動UIView的其他元素。

我做錯了什么? 沒有鍵盤:

在此處輸入圖片說明

請注意,顯示鍵盤時,日期和公司名稱是如何在UITextFields后面上推的

在此處輸入圖片說明

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

您覆蓋_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;
        }];
}

這樣可以嗎

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM