// // KeyboardViewController+Layout.m // CustomKeyboard // // Created by Codex on 2026/02/22. // #import "KeyboardViewController+Private.h" // 以 375 宽设计稿为基准的键盘总高度 static const CGFloat kKBKeyboardBaseHeight = 250.0f; static const CGFloat kKBChatPanelHeight = 180; @implementation KeyboardViewController (Layout) - (CGFloat)kb_portraitWidth { CGSize s = [UIScreen mainScreen].bounds.size; return MIN(s.width, s.height); } - (CGFloat)kb_keyboardHeightForWidth:(CGFloat)width { if (width <= 0) { width = KB_DESIGN_WIDTH; } CGFloat scale = width / KB_DESIGN_WIDTH; CGFloat baseHeight = kKBKeyboardBaseHeight * scale; CGFloat chatHeight = kKBChatPanelHeight * scale; if (self.chatPanelVisible) { return baseHeight + chatHeight; } return baseHeight; } - (CGFloat)kb_keyboardBaseHeightForWidth:(CGFloat)width { if (width <= 0) { width = KB_DESIGN_WIDTH; } CGFloat scale = width / KB_DESIGN_WIDTH; return kKBKeyboardBaseHeight * scale; } - (CGFloat)kb_chatPanelHeightForWidth:(CGFloat)width { if (width <= 0) { width = KB_DESIGN_WIDTH; } CGFloat scale = width / KB_DESIGN_WIDTH; return kKBChatPanelHeight * scale; } - (void)kb_updateKeyboardLayoutIfNeeded { CGFloat portraitWidth = [self kb_portraitWidth]; CGFloat keyboardHeight = [self kb_keyboardHeightForWidth:portraitWidth]; CGFloat keyboardBaseHeight = [self kb_keyboardBaseHeightForWidth:portraitWidth]; CGFloat chatPanelHeight = [self kb_chatPanelHeightForWidth:portraitWidth]; CGFloat containerWidth = CGRectGetWidth(self.view.superview.bounds); if (containerWidth <= 0) { containerWidth = CGRectGetWidth(self.view.window.bounds); } if (containerWidth <= 0) { containerWidth = CGRectGetWidth([UIScreen mainScreen].bounds); } BOOL widthChanged = (fabs(self.kb_lastPortraitWidth - portraitWidth) >= 0.5); BOOL heightChanged = (fabs(self.kb_lastKeyboardHeight - keyboardHeight) >= 0.5); if (!widthChanged && !heightChanged && containerWidth > 0 && self.kb_widthConstraint.constant == containerWidth) { return; } self.kb_lastPortraitWidth = portraitWidth; self.kb_lastKeyboardHeight = keyboardHeight; if (self.kb_heightConstraint) { self.kb_heightConstraint.constant = keyboardHeight; } if (containerWidth > 0 && self.kb_widthConstraint) { self.kb_widthConstraint.constant = containerWidth; } if (self.contentWidthConstraint) { [self.contentWidthConstraint setOffset:portraitWidth]; } if (self.contentHeightConstraint) { [self.contentHeightConstraint setOffset:keyboardHeight]; } if (self.keyBoardMainHeightConstraint) { [self.keyBoardMainHeightConstraint setOffset:keyboardBaseHeight]; } if (self.chatPanelHeightConstraint) { [self.chatPanelHeightConstraint setOffset:chatPanelHeight]; } [self.view layoutIfNeeded]; } @end