当键盘出现在 iOS 中时,将 UIView 上移

Posted

技术标签:

【中文标题】当键盘出现在 iOS 中时,将 UIView 上移【英文标题】:Move UIView up when the keyboard appears in iOS 【发布时间】:2012-07-02 04:54:58 【问题描述】:

我有一个 UIView,它不在 UIScrollView 里面。当键盘出现时,我想上移我的视图。在我尝试使用此解决方案之前:How can I make a UITextField move up when the keyboard is present?.

工作正常。但是在文本字段中插入数据后,它会将我带到另一个视图,当我回到这个页面时,它只是在跳跃,我看不到我的文本字段。有没有更好的办法解决这个问题?

【问题讨论】:

【参考方案1】:
- (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)viewWillDisappear:(BOOL)animated 
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];


#pragma mark - keyboard movements
- (void)keyboardWillShow:(NSNotification *)notification

    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    [UIView animateWithDuration:0.3 animations:^
        CGRect f = self.view.frame;
        f.origin.y = -keyboardSize.height;
        self.view.frame = f;
    ];


-(void)keyboardWillHide:(NSNotification *)notification

    [UIView animateWithDuration:0.3 animations:^
        CGRect f = self.view.frame;
        f.origin.y = 0.0f;
        self.view.frame = f;
    ];

【讨论】:

这对我来说效果很好,我将它与这个答案 link 结合起来,让键盘在用户点击屏幕上的其他任何地方时隐藏。 如果用户更改为另一个不同高度的键盘(如表情符号),它不起作用。要修复它,在 keyboardWillShow 中你应该使用: CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 点赞。这个解决方案比 Apple 提供的要简单得多。 Apple 解决方案无法解决我的问题,但您的问题很快。谢谢。 这项工作。但问题它也推动顶视图变得不可见。顶视图如何下移? 这是一个非常不错的解决方案,但我建议不要将视图向上移动太多,而是先检查文本字段是否隐藏,然后将其移动隐藏的数量。【参考方案2】:

使用以下代码来显示和隐藏键盘

//Declare a delegate, assign your textField to the delegate and then include these methods

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    return YES;



- (BOOL)textFieldShouldEndEditing:(UITextField *)textField 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

    [self.view endEditing:YES];
    return YES;



- (void)keyboardDidShow:(NSNotification *)notification

    // Assign new frame to your view 
    [self.view setFrame:CGRectMake(0,-110,320,460)]; //here taken -110 for example i.e. your view will be scrolled to -110. change its value according to your requirement.



-(void)keyboardDidHide:(NSNotification *)notification

    [self.view setFrame:CGRectMake(0,0,320,460)];

【讨论】:

键盘的大小应该由随键盘通知传递的 infoDictionary 确定,这应该用于计算适当的 yOffset 而不是静态定义一个。 这不能正确处理键盘更改通知,这意味着它不考虑用户何时在键盘语言之间切换以及更改键盘高度(在日文汉字键盘中点击表情符号按钮一个例子)。 感谢@Ani Shroff 的解决方案。向上运动并不平滑,它更像是一种离散运动。你能帮忙吗? 我认为不需要这些通知,您可以简单地调用 textFieldShouldBeginEditing 中的 keyboardDidShow 和 textFieldShouldEndEditing 中的 keyboardDidHide 是我还是这个方法有大问题。每次在您的文本字段中开始或结束编辑时,您都会订阅通知。当你的布局加载时,订阅应该完成一次。【参考方案3】:

我在UIView 上写了一个小类别,用于管理临时滚动内容,而无需将整个内容包装到UIScrollView 中。我在这里使用动词“滚动”可能并不理想,因为它可能会让您认为涉及滚动视图,但实际上并没有——我们只是为UIView(或UIView 子类)的位置设置动画.

其中嵌入了许多适合我的表单和布局的神奇数字,但可能不适合您的,因此我鼓励对其进行调整以满足您的特定需求。

UIView+FormScroll.h:

#import <Foundation/Foundation.h>

@interface UIView (FormScroll) 

-(void)scrollToY:(float)y;
-(void)scrollToView:(UIView *)view;
-(void)scrollElement:(UIView *)view toPoint:(float)y;

@end

UIView+FormScroll.m:

#import "UIView+FormScroll.h"


@implementation UIView (FormScroll)


-(void)scrollToY:(float)y


    [UIView beginAnimations:@"registerScroll" context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.4];
    self.transform = CGAffineTransformMakeTranslation(0, y);
    [UIView commitAnimations];



-(void)scrollToView:(UIView *)view

    CGRect theFrame = view.frame;
    float y = theFrame.origin.y - 15;
    y -= (y/1.7);
    [self scrollToY:-y];



-(void)scrollElement:(UIView *)view toPoint:(float)y

    CGRect theFrame = view.frame;
    float orig_y = theFrame.origin.y;
    float diff = y - orig_y;
    if (diff < 0) 
        [self scrollToY:diff];
    
    else 
        [self scrollToY:0];
    



@end

将它导入你的 UIViewController,然后你就可以做

- (void)textFieldDidBeginEditing:(UITextField *)textField

    [self.view scrollToView:textField];


-(void) textFieldDidEndEditing:(UITextField *)textField

    [self.view scrollToY:0];
    [textField resignFirstResponder];

...或其他任何东西。该类别为您提供了三种很好的方法来调整视图的位置。

【讨论】:

根据keyboardWillAppear NSNotification的infoDictionary中传递的键盘尺寸确定适当的yOffset以传递给这个类别方法。 @isaac - 绝对。可以改进代码的几种方法之一。【参考方案4】:

将视图绑定到键盘也是一个选项(请参阅答案底部的 GIF)

斯威夫特 4

使用扩展:(未经过全面测试)

extension UIView
    func bindToKeyboard()
        NotificationCenter.default.addObserver(self, selector: #selector(UIView.keyboardWillChange(notification:)), name: Notification.Name.UIKeyboardWillChangeFrame, object: nil)
    

    func unbindToKeyboard()
        NotificationCenter.default.removeObserver(self, name: Notification.Name.UIKeyboardWillChangeFrame, object: nil)
    
    @objc
    func keyboardWillChange(notification: Notification) 
        let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
        let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
        let curFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
        let targetFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        let deltaY = targetFrame.origin.y - curFrame.origin.y

        UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIViewKeyframeAnimationOptions(rawValue: curve), animations: 
            self.frame.origin.y+=deltaY

        ,completion: nil)

    

斯威夫特 2 + 3

使用扩展:

extension UIView
    func bindToKeyboard()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(UIView.keyboardWillChange(_:)), name: UIKeyboardWillChangeFrameNotification, object: nil)
    


    func keyboardWillChange(notification: NSNotification) 

        let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
        let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
        let curFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
        let targetFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
        let deltaY = targetFrame.origin.y - curFrame.origin.y


        UIView.animateKeyframesWithDuration(duration, delay: 0.0, options: UIViewKeyframeAnimationOptions(rawValue: curve), animations: 
                self.frame.origin.y+=deltaY

        ,completion: nil)

      

用法:

// view did load...
textField.bindToKeyboard()

...

// view unload
textField.unbindToKeyboard()

结果:

重要 卸载视图时不要忘记移除观察者

【讨论】:

【参考方案5】:

我发现theDuncs answer 非常有用,您可以在下面找到我自己的(重构)版本:


主要变化

    现在动态获取键盘大小,而不是硬编码值 将 UIView 动画提取到它自己的方法中到prevent duplicate code 允许将持续时间传递到方法中,而不是硬编码

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


- (void)viewWillDisappear:(BOOL)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;

    float newVerticalPosition = -keyboardSize.height;

    [self moveFrameToVerticalPosition:newVerticalPosition forDuration:0.3f];



- (void)keyboardWillHide:(NSNotification *)notification 
    [self moveFrameToVerticalPosition:0.0f forDuration:0.3f];



- (void)moveFrameToVerticalPosition:(float)position forDuration:(float)duration 
    CGRect frame = self.view.frame;
    frame.origin.y = position;

    [UIView animateWithDuration:duration animations:^
        self.view.frame = frame;
    ];

【讨论】:

frame.origin.y = frame.origin.y - keyboardSize.height;否则键盘会跳出屏幕。 正是我想要的,除非你按下“听写”按钮,否则视图将永远不会再次出现,因为你使用的是UIKeyboardFrameBeginUserInfoKey而不是UIKeyboardFrameEndUserInfoKey。你应该编辑它,否则完美! 我已经修改了我的答案以包含@damjandd的建议 - 谢谢! 修改self.view.frame后还需要[self.view layoutSubviews]。如果@view 包含 tableTable,这将修复 iphone X 上的安全边距问题。【参考方案6】:

基于 Daniel Krom 的解决方案。这是 swift 3.0 中的版本。与 AutoLayout 配合使用效果很好,并在键盘出现时移动整个视图。

extension UIView 

    func bindToKeyboard()
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
    

    func unbindFromKeyboard()
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
    

    @objc
    func keyboardWillChange(notification: NSNotification) 

        guard let userInfo = notification.userInfo else  return 

        let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double
        let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as! UInt
        let curFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
        let targetFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        let deltaY = targetFrame.origin.y - curFrame.origin.y

        UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIViewKeyframeAnimationOptions(rawValue: curve), animations: 
            self.frame.origin.y += deltaY
        )
      

使用方法: 在viewDidLoad中添加bindToKeyboard函数like:

override func viewDidLoad() 
    super.viewDidLoad()

    view.bindToKeyboard()

并在deinit中添加unbindFromKeyboard函数:

deinit 
    view.unbindFromKeyboard()

【讨论】:

你在objective c中有这个吗? @Moxarth 抱歉,但我没有。在 Objective-C 中,您可以创建类别。这是解释如何做的链接:developer.apple.com/library/content/documentation/Cocoa/… 整个视图在视图和键盘之间保持黑色视图。 在 iPhone X 上失败。【参考方案7】:

斯威夫特 5

上面 Daniel Krom 的答案更新版本:

extension UIView 

    func bindToKeyboard() 
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(UIView.keyboardWillChange(notification:)),
            name: UIResponder.keyboardWillChangeFrameNotification,
            object: nil
        )
    

    func unbindToKeyboard() 
        NotificationCenter.default.removeObserver(
            self,
            name: UIResponder.keyboardWillChangeFrameNotification,
            object: nil
        )
    

    @objc func keyboardWillChange(notification: Notification) 
        let duration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
        let curve = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! UInt
        let curFrame = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
        let targetFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        let deltaY = targetFrame.origin.y - curFrame.origin.y

        UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: 
            self.frame.origin.y += deltaY
        )
    


【讨论】:

【参考方案8】:

对于所有与 KeyBoard 相关的问题,只需使用 IQKeyBoardManager 这很有帮助。 https://github.com/hackiftekhar/IQKeyboardManager.

【讨论】:

【参考方案9】:

试试这个:-

 [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector (keyboardDidShow:)
                                                 name: UIKeyboardDidShowNotification object:nil];


[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector (keyboardDidHide:)
                                                 name: UIKeyboardDidHideNotification object:nil];

-(void) keyboardDidShow: (NSNotification *)notif
    
        CGSize keyboardSize = [[[notif userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height+[self getTableView].tableFooterView.frame.size.height, 0.0);

        [self getTableView].contentInset = contentInsets;
        [self getTableView].scrollIndicatorInsets = contentInsets;

        CGRect rect = self.frame; rect.size.height -= keyboardSize.height;
        if (!CGRectContainsPoint(rect, self.frame.origin))
        
            CGPoint scrollPoint = CGPointMake(0.0, self.frame.origin.y - (keyboardSize.height - self.frame.size.height));
            [[self getTableView] setContentOffset:scrollPoint animated:YES];
        
    

-(void) keyboardDidHide: (NSNotification *)notif

    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    [self getTableView].contentInset = contentInsets;
    [self getTableView].scrollIndicatorInsets = contentInsets;

【讨论】:

【参考方案10】:

基于theDunc's answer,但使用 Swift 编写并带有 Autolayout。

@IBOutlet weak var bottomConstraint: NSLayoutConstraint! // connect the bottom of the view you want to move to the bottom layout guide

override func viewWillAppear(animated: Bool) 
    super.viewWillAppear(animated)
    NSNotificationCenter.defaultCenter().addObserver(self,
                                                     selector: #selector(ConversationViewController.keyboardWillShow(_:)),
                                                     name: UIKeyboardWillShowNotification,
                                                     object: nil)

    NSNotificationCenter.defaultCenter().addObserver(self,
                                                     selector: #selector(ConversationViewController.keyboardWillHide(_:)),
                                                     name: UIKeyboardWillHideNotification,
                                                     object: nil)


override func viewWillDisappear(animated: Bool) 
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
    super.viewWillDisappear(animated)


// MARK: - Keyboard events

func keyboardWillShow(notification: NSNotification) 
    if let userInfo = notification.userInfo,
        keyboardFrame = userInfo[UIKeyboardFrameBeginUserInfoKey]
    
        let keyboardSize = keyboardFrame.CGRectValue().size
        self.bottomConstraint.constant = keyboardSize.height
        UIView.animateWithDuration(0.3) 
            self.view.layoutIfNeeded()
        
    


func keyboardWillHide(notification: NSNotification) 
    self.bottomConstraint.constant = 0
    UIView.animateWithDuration(0.3) 
        self.view.layoutIfNeeded()
    

【讨论】:

【参考方案11】:

我已经实现了一个自定义控制器,它可以动态计算键盘的大小,当它出现和消失时滚动文本字段,即使在设备旋转期间也是如此。适用于所有 ios 设备。只需简单地继承控制器即可拥有您需要的东西。您可以在以下链接中找到所有说明:https://github.com/mikthebig/ios-textfield-scroll

【讨论】:

刚刚为您的出色解决方案打开了一个拉取请求,因此它还可以支持 UITextView 类型的输入字段,而不仅仅是 UITextField:github.com/mikthebig/ios-textfield-scroll/pulls【参考方案12】:

不添加观察者通知的简单解决方案

-(void)setViewMovedUp:(BOOL)movedUp

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:0.3]; // if you want to slide up the view

    CGRect rect = self.view.frame;

    if (movedUp)
    
        // 1. move the view's origin up so that the text field that will be hidden come above the keyboard
        // 2. increase the size of the view so that the area behind the keyboard is covered up.
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
        rect.size.height += kOFFSET_FOR_KEYBOARD;
    
    else
    
        // revert back to the normal state.
        rect.origin.y += kOFFSET_FOR_KEYBOARD;
        rect.size.height -= kOFFSET_FOR_KEYBOARD;
    
    self.view.frame = rect;

    [UIView commitAnimations];



-(void)textFieldDidEndEditing:(UITextField *)sender

     if  (self.view.frame.origin.y >= 0)
        
            [self setViewMovedUp:NO];
        


-(void)textFieldDidBeginEditing:(UITextField *)sender

        //move the main view, so that the keyboard does not hide it.
        if  (self.view.frame.origin.y >= 0)
        
            [self setViewMovedUp:YES];
        

在哪里

#define kOFFSET_FOR_KEYBOARD 80.0

【讨论】:

不要使用此代码 - 没有设置 kOFFSET_FOR_KEYBOARD 对所有语言、设备和方向都有效。从 UIKeyboardWillShowNotification 和 UIKeyboardWillHideNotification 获取键盘高度和外观动画持续时间。【参考方案13】:

斯威夫特 4

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange), name: .UIKeyboardWillChangeFrame, object: nil)

@objc func keyboardWillChange(notification: NSNotification) 

        let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
        let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
        let curFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
        let targetFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        let deltaY = targetFrame.origin.y - curFrame.origin.y

        UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIViewKeyframeAnimationOptions(rawValue: curve), animations: 
            self.YourView.frame.origin.y+=deltaY
        ,completion: nil)
    

【讨论】:

【参考方案14】:

以防万一有人在 Swift 中寻找解决方案,请将其放入您的代码中:

override func viewWillAppear(_ animated: Bool) 
    super.viewWillAppear(animated)

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: .UIKeyboardWillHide, object: nil)


override func viewWillDisappear(_ animated: Bool) 
    super.viewWillDisappear(animated)

    NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
    NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)



@objc func keyboardWillShow(notification: Notification) 
    if let userInfo = notification.userInfo 
        if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue 
            UIView.animate(withDuration: 0.3) 
                var alteredFrame = self.view.frame
                alteredFrame.origin.y = -keyboardSize.height
                self.view.frame = alteredFrame
            
        
    


@objc func keyboardWillHide(notification: Notification) 
    UIView.animate(withDuration: 0.3) 
        var alteredFrame = self.view.frame
        alteredFrame.origin.y = 0.0
        self.view.frame = alteredFrame
    

【讨论】:

【参考方案15】:

给你。不过,我已将此代码与 UIView 一起使用。您应该能够对滚动视图进行这些调整。

    func addKeyboardNotifications() 
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(keyboardWillShow(notification:)),
                                               name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(keyboardWillHide(notification:)),
                                               name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    

    func keyboardWillShow(notification: NSNotification) 

        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue 
            let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
// if using constraints            
// bottomViewBottomSpaceConstraint.constant = keyboardSize.height
self.view.frame.origin.y -= keyboardSize.height
            UIView.animate(withDuration: duration) 
                self.view.layoutIfNeeded()
            
        
    
    func keyboardWillHide(notification: NSNotification) 

        let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
//if using constraint
//        bottomViewBottomSpaceConstraint.constant = 0
self.view.frame.origin.y = 0
        UIView.animate(withDuration: duration) 
            self.view.layoutIfNeeded()
        
    

不要忘记在正确的位置删除通知。

func removeKeyboardNotifications() 
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)

【讨论】:

【参考方案16】:

我刚刚创建了一个轻量级键盘处理程序来跟随键盘框架。

用法:

 self.keyboardHandler = [EDKeyboardHandler new];

  [self.keyboardHandler listenWithBlock:^(KeyboardInfo *model)
  
    //adjust view positions according to keyboard position here
  ];

KeyboardInfo 模型具有以下属性:

typedef enum : NSUInteger 
    KeyboardStatusDidShow,
    KeyboardStatusWillShow,
    KeyboardStatusDidHide,
    KeyboardStatusWillHide,
 KeyboardStatus;

@interface KeyboardInfo:NSObject

@property (nonatomic,readonly) NSTimeInterval animationDuration;
@property (nonatomic,readonly) CGRect keyboardFrame;
@property (nonatomic,readonly) NSInteger animationCurve;
@property (nonatomic,readonly) KeyboardStatus status;

@end

查看GitHub 项目以了解详细信息和 cocoaPods 集成。

【讨论】:

【参考方案17】:

声明一个委托,将您的文本字段分配给委托,然后包含这些方法。

假设您有一个包含电子邮件和密码文本字段的登录表单,此代码将非常适合:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

    [self.emailTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];



- (BOOL)textFieldShouldReturn:(UITextField *)textField 

    if (self.emailTextField == textField) 
        [self.passwordTextField becomeFirstResponder];
     else 
        [self.emailTextField resignFirstResponder];
        [self.passwordTextField resignFirstResponder];
    
    return NO;

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


- (void)viewWillDisappear:(BOOL)animated 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];


#pragma mark - keyboard movements
- (void)keyboardWillShow:(NSNotification *)notification

    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    [UIView animateWithDuration:0.3 animations:^
        CGRect f = self.view.frame;
        f.origin.y = -0.5f * keyboardSize.height;
        self.view.frame = f;
    ];


-(void)keyboardWillHide:(NSNotification *)notification

    [UIView animateWithDuration:0.3 animations:^
        CGRect f = self.view.frame;
        f.origin.y = 0.0f;
        self.view.frame = f;
    ];

【讨论】:

[super viewWillDisappear:animated]; 呢? 这是为了移除在 viewWillAppear 中添加的观察者。【参考方案18】:

如果textfield 在表格的单元格中(即使table.scrollable = NO),它可以轻松自动地完成

注意:桌子的位置和大小必​​须合理。例如:

如果表格的 y 位置从视图底部算起 100,则 300 高度的键盘将覆盖整个表格。 如果表格的height = 10 和其中的textfield 在键盘出现时必须向上滚动100 才能可见,那么该文本字段将超出表格的范围。

【讨论】:

以上是关于当键盘出现在 iOS 中时,将 UIView 上移的主要内容,如果未能解决你的问题,请参考以下文章

键盘出现时UITextField上移【Swift】

出现键盘时无法上移自定义 UIView

iOS开发-cell里面有textField,出现键盘自动上移

当 SnackBar 出现在 CoordinatorLayout 中时上移视图

当键盘出现在 iOS 中时启用滚动内容

iOS-当输入框被键盘遮挡时让整个view上移