修改了横屏键盘不居中为题

This commit is contained in:
2026-01-07 13:11:23 +08:00
parent c3909d63da
commit 0965cd3c7e
3 changed files with 116 additions and 30 deletions

View File

@@ -47,6 +47,7 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center,
@interface KeyboardViewController () <KBKeyBoardMainViewDelegate, KBFunctionViewDelegate, KBKeyboardSubscriptionViewDelegate> @interface KeyboardViewController () <KBKeyBoardMainViewDelegate, KBFunctionViewDelegate, KBKeyboardSubscriptionViewDelegate>
@property (nonatomic, strong) UIButton *nextKeyboardButton; // @property (nonatomic, strong) UIButton *nextKeyboardButton; //
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) KBKeyBoardMainView *keyBoardMainView; // 0 @property (nonatomic, strong) KBKeyBoardMainView *keyBoardMainView; // 0
@property (nonatomic, strong) KBFunctionView *functionView; // 0 @property (nonatomic, strong) KBFunctionView *functionView; // 0
@property (nonatomic, strong) KBSettingView *settingView; // @property (nonatomic, strong) KBSettingView *settingView; //
@@ -55,6 +56,12 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center,
@property (nonatomic, strong) KBSuggestionEngine *suggestionEngine; @property (nonatomic, strong) KBSuggestionEngine *suggestionEngine;
@property (nonatomic, copy) NSString *currentWord; @property (nonatomic, copy) NSString *currentWord;
@property (nonatomic, assign) BOOL suppressSuggestions; @property (nonatomic, assign) BOOL suppressSuggestions;
@property (nonatomic, strong) MASConstraint *contentWidthConstraint;
@property (nonatomic, strong) MASConstraint *contentHeightConstraint;
@property (nonatomic, strong) NSLayoutConstraint *kb_heightConstraint;
@property (nonatomic, strong) NSLayoutConstraint *kb_widthConstraint;
@property (nonatomic, assign) CGFloat kb_lastPortraitWidth;
@property (nonatomic, assign) CGFloat kb_lastKeyboardHeight;
@end @end
@implementation KeyboardViewController @implementation KeyboardViewController
@@ -119,13 +126,16 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center,
- (void)setupUI { - (void)setupUI {
self.view.translatesAutoresizingMaskIntoConstraints = NO; self.view.translatesAutoresizingMaskIntoConstraints = NO;
// / //
CGFloat keyboardHeight = KBFit(kKBKeyboardBaseHeight); CGFloat portraitWidth = [self kb_portraitWidth];
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width; CGFloat keyboardHeight = [self kb_keyboardHeightForWidth:portraitWidth];
CGFloat screenWidth = CGRectGetWidth([UIScreen mainScreen].bounds);
CGFloat outerVerticalInset = KBFit(4.0f); CGFloat outerVerticalInset = KBFit(4.0f);
NSLayoutConstraint *h = [self.view.heightAnchor constraintEqualToConstant:keyboardHeight]; NSLayoutConstraint *h = [self.view.heightAnchor constraintEqualToConstant:keyboardHeight];
NSLayoutConstraint *w = [self.view.widthAnchor constraintEqualToConstant:screenWidth]; NSLayoutConstraint *w = [self.view.widthAnchor constraintEqualToConstant:screenWidth];
self.kb_heightConstraint = h;
self.kb_widthConstraint = w;
h.priority = UILayoutPriorityRequired; h.priority = UILayoutPriorityRequired;
w.priority = UILayoutPriorityRequired; w.priority = UILayoutPriorityRequired;
@@ -137,25 +147,30 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center,
iv.allowsSelfSizing = NO; iv.allowsSelfSizing = NO;
} }
} }
// //
[self.view addSubview:self.bgImageView]; [self.view addSubview:self.contentView];
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.bottom.equalTo(self.view);
self.contentWidthConstraint = make.width.mas_equalTo(portraitWidth);
self.contentHeightConstraint = make.height.mas_equalTo(keyboardHeight);
}];
//
[self.contentView addSubview:self.bgImageView];
[self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) { [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view); make.edges.equalTo(self.contentView);
}]; }];
// //
self.functionView.hidden = YES; self.functionView.hidden = YES;
[self.view addSubview:self.functionView]; [self.contentView addSubview:self.functionView];
[self.functionView mas_makeConstraints:^(MASConstraintMaker *make) { [self.functionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view); make.edges.equalTo(self.contentView);
make.top.equalTo(self.view).offset(0);
make.bottom.equalTo(self.view).offset(0);
}]; }];
[self.view addSubview:self.keyBoardMainView]; [self.contentView addSubview:self.keyBoardMainView];
[self.keyBoardMainView mas_makeConstraints:^(MASConstraintMaker *make) { [self.keyBoardMainView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view); make.edges.equalTo(self.contentView);
make.top.equalTo(self.view).offset(0);
make.bottom.equalTo(self.view.mas_bottom).offset(-0);
}]; }];
} }
@@ -291,9 +306,9 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center,
// //
if (show) { if (show) {
[self.view bringSubviewToFront:self.functionView]; [self.contentView bringSubviewToFront:self.functionView];
} else { } else {
[self.view bringSubviewToFront:self.keyBoardMainView]; [self.contentView bringSubviewToFront:self.keyBoardMainView];
} }
} }
@@ -307,19 +322,19 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center,
// if (!self.settingView) { // if (!self.settingView) {
self.settingView = [[KBSettingView alloc] init]; self.settingView = [[KBSettingView alloc] init];
self.settingView.hidden = YES; self.settingView.hidden = YES;
[self.view addSubview:self.settingView]; [self.contentView addSubview:self.settingView];
[self.settingView mas_makeConstraints:^(MASConstraintMaker *make) { [self.settingView mas_makeConstraints:^(MASConstraintMaker *make) {
// //
make.edges.equalTo(self.keyBoardMainView); make.edges.equalTo(self.contentView);
}]; }];
[self.settingView.backButton addTarget:self action:@selector(onTapSettingsBack) forControlEvents:UIControlEventTouchUpInside]; [self.settingView.backButton addTarget:self action:@selector(onTapSettingsBack) forControlEvents:UIControlEventTouchUpInside];
// } // }
[self.view bringSubviewToFront:self.settingView]; [self.contentView bringSubviewToFront:self.settingView];
// keyBoardMainView self.view // keyBoardMainView self.view
[self.view layoutIfNeeded]; [self.contentView layoutIfNeeded];
CGFloat w = CGRectGetWidth(self.keyBoardMainView.bounds); CGFloat w = CGRectGetWidth(self.keyBoardMainView.bounds);
if (w <= 0) { w = CGRectGetWidth(self.view.bounds); } if (w <= 0) { w = CGRectGetWidth(self.contentView.bounds); }
if (w <= 0) { w = [UIScreen mainScreen].bounds.size.width; } if (w <= 0) { w = [self kb_portraitWidth]; }
self.settingView.transform = CGAffineTransformMakeTranslation(w, 0); self.settingView.transform = CGAffineTransformMakeTranslation(w, 0);
self.settingView.hidden = NO; self.settingView.hidden = NO;
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
@@ -328,8 +343,8 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center,
} else { } else {
if (!self.settingView || self.settingView.hidden) return; if (!self.settingView || self.settingView.hidden) return;
CGFloat w = CGRectGetWidth(self.keyBoardMainView.bounds); CGFloat w = CGRectGetWidth(self.keyBoardMainView.bounds);
if (w <= 0) { w = CGRectGetWidth(self.view.bounds); } if (w <= 0) { w = CGRectGetWidth(self.contentView.bounds); }
if (w <= 0) { w = [UIScreen mainScreen].bounds.size.width; } if (w <= 0) { w = [self kb_portraitWidth]; }
[UIView animateWithDuration:0.22 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{ [UIView animateWithDuration:0.22 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.settingView.transform = CGAffineTransformMakeTranslation(w, 0); self.settingView.transform = CGAffineTransformMakeTranslation(w, 0);
} completion:^(BOOL finished) { } completion:^(BOOL finished) {
@@ -363,15 +378,15 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center,
KBKeyboardSubscriptionView *panel = self.subscriptionView; KBKeyboardSubscriptionView *panel = self.subscriptionView;
if (!panel.superview) { if (!panel.superview) {
panel.hidden = YES; panel.hidden = YES;
[self.view addSubview:panel]; [self.contentView addSubview:panel];
[panel mas_makeConstraints:^(MASConstraintMaker *make) { [panel mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.keyBoardMainView); make.edges.equalTo(self.contentView);
}]; }];
} }
[self.view bringSubviewToFront:panel]; [self.contentView bringSubviewToFront:panel];
panel.hidden = NO; panel.hidden = NO;
panel.alpha = 0.0; panel.alpha = 0.0;
CGFloat height = CGRectGetHeight(self.view.bounds); CGFloat height = CGRectGetHeight(self.contentView.bounds);
if (height <= 0) { height = 260; } if (height <= 0) { height = 260; }
panel.transform = CGAffineTransformMakeTranslation(0, height); panel.transform = CGAffineTransformMakeTranslation(0, height);
[panel refreshProductsIfNeeded]; [panel refreshProductsIfNeeded];
@@ -384,7 +399,7 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center,
- (void)hideSubscriptionPanel { - (void)hideSubscriptionPanel {
if (!self.subscriptionView || self.subscriptionView.hidden) { return; } if (!self.subscriptionView || self.subscriptionView.hidden) { return; }
CGFloat height = CGRectGetHeight(self.subscriptionView.bounds); CGFloat height = CGRectGetHeight(self.subscriptionView.bounds);
if (height <= 0) { height = CGRectGetHeight(self.view.bounds); } if (height <= 0) { height = CGRectGetHeight(self.contentView.bounds); }
KBKeyboardSubscriptionView *panel = self.subscriptionView; KBKeyboardSubscriptionView *panel = self.subscriptionView;
[UIView animateWithDuration:0.22 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{ [UIView animateWithDuration:0.22 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
panel.alpha = 0.0; panel.alpha = 0.0;
@@ -679,6 +694,21 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center,
// } // }
} }
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self kb_updateKeyboardLayoutIfNeeded];
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
__weak typeof(self) weakSelf = self;
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
[weakSelf kb_updateKeyboardLayoutIfNeeded];
} completion:^(__unused id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
[weakSelf kb_updateKeyboardLayoutIfNeeded];
}];
}
//- (void)kb_tryOpenContainerForLoginIfNeeded { //- (void)kb_tryOpenContainerForLoginIfNeeded {
// // 使 App Scheme // // 使 App Scheme
// NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@@//login?src=keyboard", KB_APP_SCHEME]]; // NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@@//login?src=keyboard", KB_APP_SCHEME]];
@@ -699,6 +729,7 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center,
self.bgImageView.image = img; self.bgImageView.image = img;
BOOL hasImg = (img != nil); BOOL hasImg = (img != nil);
self.view.backgroundColor = hasImg ? [UIColor clearColor] : t.keyboardBackground; self.view.backgroundColor = hasImg ? [UIColor clearColor] : t.keyboardBackground;
self.contentView.backgroundColor = hasImg ? [UIColor clearColor] : t.keyboardBackground;
self.keyBoardMainView.backgroundColor = hasImg ? [UIColor clearColor] : t.keyboardBackground; self.keyBoardMainView.backgroundColor = hasImg ? [UIColor clearColor] : t.keyboardBackground;
// //
if ([self.keyBoardMainView respondsToSelector:@selector(kb_applyTheme)]) { if ([self.keyBoardMainView respondsToSelector:@selector(kb_applyTheme)]) {
@@ -732,8 +763,62 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center,
}]; }];
} }
#pragma mark - Layout Helpers
- (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; }
return kKBKeyboardBaseHeight * (width / KB_DESIGN_WIDTH);
}
- (void)kb_updateKeyboardLayoutIfNeeded {
CGFloat portraitWidth = [self kb_portraitWidth];
CGFloat keyboardHeight = [self kb_keyboardHeightForWidth: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];
}
[self.view layoutIfNeeded];
}
#pragma mark - Lazy #pragma mark - Lazy
- (UIView *)contentView {
if (!_contentView) {
_contentView = [[UIView alloc] init];
_contentView.backgroundColor = [UIColor clearColor];
}
return _contentView;
}
- (UIImageView *)bgImageView { - (UIImageView *)bgImageView {
if (!_bgImageView) { if (!_bgImageView) {
_bgImageView = [[UIImageView alloc] init]; _bgImageView = [[UIImageView alloc] init];

Binary file not shown.

View File

@@ -88,7 +88,8 @@
#if __OBJC__ #if __OBJC__
static inline CGFloat KBScreenWidth(void) { static inline CGFloat KBScreenWidth(void) {
return [UIScreen mainScreen].bounds.size.width; CGSize size = [UIScreen mainScreen].bounds.size;
return MIN(size.width, size.height);
} }
static inline CGFloat KBScaleFactor(void) { static inline CGFloat KBScaleFactor(void) {