LHSKeyboardAdjusting 2.0.1

LHSKeyboardAdjusting 2.0.1

测试已测试
Lang语言 Obj-CObjective C
许可证 Apache 2
发布最后发布2016年7月

Dan Loewenherz维护。



  • 作者
  • Dan Loewenherz

有关本项目的 Swift 端口,请参阅 KeyboardAdjuster

LHSKeyboardAdjusting

LHSKeyboardAdjusting 会通过 Auto Layout 调整当屏幕上出现键盘时您的 UIScrollView 或 UITableView 的底部位置。您只需向 LHSKeyboardAdjusting 提供一个将您的视图的底部与屏幕底部相连的 NSLayoutConstraint,LHSKeyboardAdjusting 会自动调整该约束并将其固定到键盘顶部。

LHSKeyboardAdjusting 需要在您的构建目标中使用 Auto Layout,因此它仅适用于 iOS 6 及以上版本。

注意

如果您目前在项目中使用的是 LHSKeyboardAdjusting 1.0+,请注意某些内容已更改。


安装

LHSKeyboardAdjusting 使用 CocoaPods,因此只需在 Podfile 中添加类似以下内容即可:

pod 'LHSKeyboardAdjusting', '~> 2.0'

然后运行 pod install,您就可以开始使用了。

如果您不使用 CocoaPods,将 LHSKeyboardAdjusting 文件夹拖放到 Xcode 项目中也可以。

使用方法

  1. 在您的视图控制器头文件中,将 LHSKeyboardAdjusting 协议分配给您的类,并定义一个名为 keyboardAdjustingBottomConstraintNSLayoutConstraint 属性。

    #import <LHSKeyboardAdjusting/LHSKeyboardAdjusting.h>
    
    @interface ViewController : UIViewController <LHSKeyboardAdjusting>
    
    @property (nonatomic, strong) NSLayoutConstraint *keyboardAdjustingBottomConstraint;
    
    @end
  2. 在您的视图控制器中,实现 keyboardAdjustingView 返回您想要将其固定到键盘顶部的视图。这可以是任何东西,但通常情况下是 UIScrollView、UITableView 或 UITextView。

    #pragma mark - LHSKeyboardAdjusting
    
    - (UIView *)keyboardAdjustingView {
       return self.tableView;
    }
  3. viewWillAppearviewWillDisappear 中激活和停用自动调整。

    #import <LHSKeyboardAdjusting/UIViewController+LHSKeyboardAdjustment.h>
    
    @implementation ViewController
    
    - (void)viewWillAppear:(BOOL)animated {
       [super viewWillAppear:animated];
    
       [self lhs_activateKeyboardAdjustment];
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
       [super viewWillDisappear:animated];
    
       [self lhs_deactivateKeyboardAdjustment];
    }
    
    @end

    注意:您也可以使用 lhs_activateKeyboardAdjustmentWithShow:hide: 定义键盘出现或消失时的回调。

    - (void)viewWillAppear:(BOOL)animated {
       [super viewWillAppear:animated];
    
       [self lhs_activateKeyboardAdjustmentWithShow:^{
           NSLog(@"hi");
       } hide:^{
           NSLog(@"bai");
       }];
    }
  4. 完成了!只要键盘出现,此视图就会自动调整大小。

如果您需要有关如何设置的更多提示,请克隆此代码库,并查看包含的示例项目(LHSKeyboardAdjustingExample)。

许可证

Apache 2.0,请参阅 LICENSE 文件以获取详细信息。