iOS 根据键盘弹出调整view的高度

iOS 根据键盘弹出调整view的高度

效果图:


iOS 根据键盘弹出调整view的高度_第1张图片
gif.gif

其实实现键盘高度的弹出,让咱们的界面view始终在键盘的上面,实现非常简单.
首先要知道,苹果有一些监听键盘状态的各种key

UIKIT_EXTERN NSString *const UIKeyboardWillShowNotification __TVOS_PROHIBITED;
UIKIT_EXTERN NSString *const UIKeyboardDidShowNotification __TVOS_PROHIBITED;
UIKIT_EXTERN NSString *const UIKeyboardWillHideNotification __TVOS_PROHIBITED;
UIKIT_EXTERN NSString *const UIKeyboardDidHideNotification __TVOS_PROHIBITED;

UIKIT_EXTERN NSString *const UIKeyboardFrameBeginUserInfoKey        NS_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED; // NSValue of CGRect
UIKIT_EXTERN NSString *const UIKeyboardFrameEndUserInfoKey          NS_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED; // NSValue of CGRect
UIKIT_EXTERN NSString *const UIKeyboardAnimationDurationUserInfoKey NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; // NSNumber of double
UIKIT_EXTERN NSString *const UIKeyboardAnimationCurveUserInfoKey    NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; // NSNumber of NSUInteger (UIViewAnimationCurve)
UIKIT_EXTERN NSString *const UIKeyboardIsLocalUserInfoKey           NS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED; // NSNumber of BOOL

// Like the standard keyboard notifications above, these additional notifications include
// a nil object and begin/end frames of the keyboard in screen coordinates in the userInfo dictionary.
UIKIT_EXTERN NSString *const UIKeyboardWillChangeFrameNotification  NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED;
UIKIT_EXTERN NSString *const UIKeyboardDidChangeFrameNotification   NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED;

// These keys are superseded by UIKeyboardFrameBeginUserInfoKey and UIKeyboardFrameEndUserInfoKey.
UIKIT_EXTERN NSString *const UIKeyboardCenterBeginUserInfoKey   NS_DEPRECATED_IOS(2_0, 3_2) __TVOS_PROHIBITED;
UIKIT_EXTERN NSString *const UIKeyboardCenterEndUserInfoKey     NS_DEPRECATED_IOS(2_0, 3_2) __TVOS_PROHIBITED;
UIKIT_EXTERN NSString *const UIKeyboardBoundsUserInfoKey        NS_DEPRECATED_IOS(2_0, 3_2) __TVOS_PROHIBITED;

这里我们用 UIKeyboardWillChangeFrameNotification来监听键盘frame的改变
1:添加通知的观察者

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
//当观察到键盘发生变化的通知后,就调用的方法
- (void)keyBoardFrameChange:(NSNotification *)userInfo{
//        UIKeyboardAnimationCurveUserInfoKey = 7;
//    UIKeyboardAnimationDurationUserInfoKey = "0.25";
//    UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";
//    UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}";
//    UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";
//    UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}";
//    UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";
//    UIKeyboardIsLocalUserInfoKey = 1;
    
// {
//        UIKeyboardAnimationCurveUserInfoKey = 7;
//        UIKeyboardAnimationDurationUserInfoKey = "0.25";
//        UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";
//        UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 538}";
//        UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 796}";
//        UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";
//        UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 667}, {375, 258}}";
//        UIKeyboardIsLocalUserInfoKey = 1;
//    }
//   1.修改试图的transform属性,让视图跟着键盘变化而变化
//    ty = - 258 = 409 - view的高度;:表示键盘弹出的时候的值的变化
//    ty = 0 意:transform是一种状态,当回到初始值状态为0,表示键盘回到原来的位置
//    self.view.transform ==
//    1.1获取变化的值
    NSDictionary *keyBoardDict = userInfo.userInfo;
    CGRect endKeyBoardFrame = [keyBoardDict[UIKeyboardFrameEndUserInfoKey]CGRectValue];
//这里减去的是你需要调整的view的高度,如果view是全屏幕的可以用此方法
    CGFloat ty = endKeyBoardFrame.origin.y - [UIScreen mainScreen].bounds.size.height;
//   1.2修改transform属性,让视图变化
//   1.3让修改有动画,获取动画的时间
    CGFloat duration = [keyBoardDict[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
    [UIView animateWithDuration:duration animations:^{
         self.view.transform = CGAffineTransformMakeTranslation(0, ty);
    }];
   
    
    
}

也可以学以致用,调整tableView的某一个cell可以在输入时到键盘上面

最后介绍一个强大的第三方

IQKeyboardManager
关于使用IQKeyboardManager的一些坑->(引用)

你可能感兴趣的:(iOS 根据键盘弹出调整view的高度)