在iOS objective-c中粘贴键盘的自定义UIView

时间:2022-09-07 11:12:14

I want to make a custom UIView that sticks to the keyboard, I used input Accessory view, but it doesn't stay on my view controller when the keyboard is away. How can I have a UIView that I've constrained to the bottom of my view controller to stick to the keyboard (like the input accessory view) in iOS Objective-C?

我想制作一个固定在键盘上的自定义UIView,我使用输入附件视图,但当键盘离开时它不会停留在我的视图控制器上。我怎么能有一个UIView,我已经限制在我的视图控制器的底部以坚持iOS Objective-C中的键盘(如输入附件视图)?

2 个解决方案

#1


2  

Add an IBOutlet for bottomConstraint of the view. I assume it is a direct subview of ViewController's view and it should be on the bottom when keyboard is hidden.

为视图的bottomConstraint添加IBOutlet。我假设它是ViewController视图的直接子视图,当键盘被隐藏时它应该在底部。

@IBOutlet weak var bottomConstraint: NSLayoutConstraint!

Subscribe to keyboard notifications in viewWillAppear:

在viewWillAppear中订阅键盘通知:

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.showKeyboard), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.hideKeyboard), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

Unsubscribe in viewDidDisappear:

取消订阅viewDidDisappear:

NotificationCenter.removeObserver(self)

Later in your ViewController:

稍后在ViewController中:

func showKeyboard(_ notification: NSNotification) {
    let info = notification.userInfo
    let rectValue = info![UIKeyboardFrameEndUserInfoKey] as? NSValue
    if let keyboardSize = rectValue?.cgRectValue.size {
        bottomConstraint.constant = (keyboardSize?.height)!
        view.layoutIfNeeded()
    }
}

func hideKeyboard(_ notification: Notification) {
    bottomConstraint.constant = 0
    view.layoutIfNeeded()
}

Objective-C:

Objective-C的:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

-(void)keyboardWillShow: (NSNotification *) notification {
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    _bottomConstraint.constant = keyboardSize.height;
    [view layoutIfNeeded];
}

-(void)keyboardWillHide: (NSNotification *) notification {
    _bottomConstraint.constant = 0;
    [view layoutIfNeeded];
}

#2


0  

You need to add inputAccessoryView to your textfield.

您需要将inputAccessoryView添加到文本字段中。

example

self.mytextField.inputAccessoryView = view;

Hope this fix your problem

希望这可以解决您的问题

#1


2  

Add an IBOutlet for bottomConstraint of the view. I assume it is a direct subview of ViewController's view and it should be on the bottom when keyboard is hidden.

为视图的bottomConstraint添加IBOutlet。我假设它是ViewController视图的直接子视图,当键盘被隐藏时它应该在底部。

@IBOutlet weak var bottomConstraint: NSLayoutConstraint!

Subscribe to keyboard notifications in viewWillAppear:

在viewWillAppear中订阅键盘通知:

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.showKeyboard), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.hideKeyboard), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

Unsubscribe in viewDidDisappear:

取消订阅viewDidDisappear:

NotificationCenter.removeObserver(self)

Later in your ViewController:

稍后在ViewController中:

func showKeyboard(_ notification: NSNotification) {
    let info = notification.userInfo
    let rectValue = info![UIKeyboardFrameEndUserInfoKey] as? NSValue
    if let keyboardSize = rectValue?.cgRectValue.size {
        bottomConstraint.constant = (keyboardSize?.height)!
        view.layoutIfNeeded()
    }
}

func hideKeyboard(_ notification: Notification) {
    bottomConstraint.constant = 0
    view.layoutIfNeeded()
}

Objective-C:

Objective-C的:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

-(void)keyboardWillShow: (NSNotification *) notification {
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    _bottomConstraint.constant = keyboardSize.height;
    [view layoutIfNeeded];
}

-(void)keyboardWillHide: (NSNotification *) notification {
    _bottomConstraint.constant = 0;
    [view layoutIfNeeded];
}

#2


0  

You need to add inputAccessoryView to your textfield.

您需要将inputAccessoryView添加到文本字段中。

example

self.mytextField.inputAccessoryView = view;

Hope this fix your problem

希望这可以解决您的问题