iOS 键盘弹出视图精准上移

1 视图精准位移,需要计算出输入框距离屏幕底部的距离,然后

计算出输入框需要上移的距离,就是整个视图需要上移的距离,

注意,我们可以自行设置一个适当的buffer,我这里是40,就是输入框距离

键盘有一个40的距离,避免紧挨着 导致体验不佳

复制代码
- (void)keyboardWillShow:(NSNotification *)noti
{
    UpdateUserInfoView  *userInforView = self.editCell.userInfoView;
    
    CGRect userInfoViewframe = [self.editCell convertRect:userInforView.frame toView:window];
    CGFloat bottomSpace = GetScreenHeight()  - CGRectGetMaxY(userInfoViewframe);
    //设置一个buffer
    bottomSpace -= 40;
    CGRect keyboardRect = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat boardHeight = keyboardRect.size.height;
    NSLog(@" keyboardWillShow editCellFrame%@ bottomSpace%f boardHeight%f", NSStringFromCGRect(userInfoViewframe), bottomSpace, boardHeight);

    CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    Weakify(self);
    [UIView animateWithDuration:duration animations:^{
        Strongify(self);
        self.backView.y -= (boardHeight - bottomSpace);
    }];
    [self.tableView addGestureRecognizer:self.endEditingTap];
}
相关推荐
CocoaKier11 小时前
神技分享:App加锁,App隐藏?苹果官方自带,只需要一个长按
ios·apple
FungLeo12 小时前
Swift macOS 游戏开发实战|空当接龙 11|使用 Swift Actor 实现自动存档与坏档保护
macos·swift·数据持久化·actor·swift并发
sean90816 小时前
macOS Outlook(或者是Office系列?) App 打开时自动启动 Microsoft Autoupdate 处理
microsoft·macos·outlook·自动更新
FungLeo16 小时前
Swift macOS 游戏开发实战|空当接龙 09|SwiftUI 与 AppKit 动画,如何分离显示与游戏状态
macos·swiftui·动画·swift·appkit
FungLeo17 小时前
Swift macOS 游戏开发实战|空当接龙 13|SwiftUI 与 AppKit 应用的多语言切换与 RTL 布局
macos·国际化·swiftui·swift·rtl
传奇开心果编程20 小时前
【六大移动UI框架比较学与练】第5课:交互与导航——事件处理、手势与页面路由实战对照
android·flutter·react native·ios·swiftui·移动开发·android jetpack
AirDroid_cn21 小时前
下棋也有“光影魔术”:macOS Sequoia国际象棋棋盘随环境光自动变质感
macos
Geek-Chow1 天前
01 · 为什么要在 iOS 上用 Redux
ios
传奇开心果编程2 天前
【六大移动UI框架比较学与练】第3课:文本、图片、布局核心组件实战对照
android·flutter·react native·ios·swiftui·移动开发·android jetpack
FungLeo2 天前
Swift macOS 游戏开发实战|空当接龙 07|使用 Swift 计算稳定牌桌布局与命中区域
macos·swift·游戏开发·界面布局·命中检测