在非刘海添加地球
This commit is contained in:
@@ -7,12 +7,14 @@
|
|||||||
|
|
||||||
#import "KBFunctionBarView.h"
|
#import "KBFunctionBarView.h"
|
||||||
#import "Masonry.h"
|
#import "Masonry.h"
|
||||||
|
#import "KBResponderUtils.h" // 查找 UIInputViewController,用于系统切换输入法
|
||||||
|
|
||||||
@interface KBFunctionBarView ()
|
@interface KBFunctionBarView ()
|
||||||
@property (nonatomic, strong) UIView *leftContainer; // 左侧按钮容器
|
@property (nonatomic, strong) UIView *leftContainer; // 左侧按钮容器
|
||||||
@property (nonatomic, strong) UIView *rightContainer; // 右侧按钮容器
|
@property (nonatomic, strong) UIView *rightContainer; // 右侧按钮容器
|
||||||
@property (nonatomic, strong) NSArray<UIButton *> *leftButtonsInternal;
|
@property (nonatomic, strong) NSArray<UIButton *> *leftButtonsInternal;
|
||||||
@property (nonatomic, strong) NSArray<UIButton *> *rightButtonsInternal;
|
@property (nonatomic, strong) NSArray<UIButton *> *rightButtonsInternal;
|
||||||
|
@property (nonatomic, strong) UIButton *globeButtonInternal; // 可选:系统“切换输入法”键
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation KBFunctionBarView
|
@implementation KBFunctionBarView
|
||||||
@@ -38,6 +40,7 @@
|
|||||||
- (void)buildUI {
|
- (void)buildUI {
|
||||||
// 左右两个容器,方便分别布局
|
// 左右两个容器,方便分别布局
|
||||||
[self addSubview:self.leftContainer];
|
[self addSubview:self.leftContainer];
|
||||||
|
[self addSubview:self.globeButtonInternal];
|
||||||
[self addSubview:self.rightContainer];
|
[self addSubview:self.rightContainer];
|
||||||
|
|
||||||
[self.rightContainer mas_makeConstraints:^(MASConstraintMaker *make) {
|
[self.rightContainer mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||||
@@ -46,8 +49,15 @@
|
|||||||
make.height.mas_equalTo(36);
|
make.height.mas_equalTo(36);
|
||||||
}];
|
}];
|
||||||
|
|
||||||
[self.leftContainer mas_makeConstraints:^(MASConstraintMaker *make) {
|
// 左侧地球键(按需显示)
|
||||||
|
[self.globeButtonInternal mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||||
make.left.equalTo(self.mas_left).offset(12);
|
make.left.equalTo(self.mas_left).offset(12);
|
||||||
|
make.centerY.equalTo(self.mas_centerY);
|
||||||
|
make.width.height.mas_equalTo(32);
|
||||||
|
}];
|
||||||
|
|
||||||
|
[self.leftContainer mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||||
|
make.left.equalTo(self.globeButtonInternal.mas_right).offset(8);
|
||||||
make.right.equalTo(self.rightContainer.mas_left).offset(-12);
|
make.right.equalTo(self.rightContainer.mas_left).offset(-12);
|
||||||
make.centerY.equalTo(self.mas_centerY);
|
make.centerY.equalTo(self.mas_centerY);
|
||||||
make.height.mas_equalTo(36);
|
make.height.mas_equalTo(36);
|
||||||
@@ -113,6 +123,9 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.rightButtonsInternal = rightBtns.copy;
|
self.rightButtonsInternal = rightBtns.copy;
|
||||||
|
|
||||||
|
// 初始刷新地球键可见性与事件绑定
|
||||||
|
[self kb_refreshGlobeVisibility];
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - Actions
|
#pragma mark - Actions
|
||||||
@@ -158,4 +171,56 @@
|
|||||||
return _rightContainer;
|
return _rightContainer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (UIButton *)globeButtonInternal {
|
||||||
|
if (!_globeButtonInternal) {
|
||||||
|
_globeButtonInternal = [UIButton buttonWithType:UIButtonTypeSystem];
|
||||||
|
_globeButtonInternal.layer.cornerRadius = 16;
|
||||||
|
_globeButtonInternal.layer.masksToBounds = YES;
|
||||||
|
_globeButtonInternal.backgroundColor = [UIColor colorWithWhite:1 alpha:0.9];
|
||||||
|
[_globeButtonInternal setTitle:@"🌐" forState:UIControlStateNormal];
|
||||||
|
[_globeButtonInternal setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
|
||||||
|
}
|
||||||
|
return _globeButtonInternal;
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma mark - Globe (Input Mode Switch)
|
||||||
|
|
||||||
|
- (void)kb_refreshGlobeVisibility {
|
||||||
|
UIInputViewController *ivc = KBFindInputViewController(self);
|
||||||
|
BOOL needSwitchKey = YES;
|
||||||
|
if (ivc && [ivc respondsToSelector:@selector(needsInputModeSwitchKey)]) {
|
||||||
|
needSwitchKey = ivc.needsInputModeSwitchKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.globeButtonInternal.hidden = !needSwitchKey;
|
||||||
|
|
||||||
|
// 左容器左约束:根据是否显示地球键动态调整
|
||||||
|
[self.leftContainer mas_remakeConstraints:^(MASConstraintMaker *make) {
|
||||||
|
if (needSwitchKey) {
|
||||||
|
make.left.equalTo(self.globeButtonInternal.mas_right).offset(8);
|
||||||
|
} else {
|
||||||
|
make.left.equalTo(self.mas_left).offset(12);
|
||||||
|
}
|
||||||
|
make.right.equalTo(self.rightContainer.mas_left).offset(-12);
|
||||||
|
make.centerY.equalTo(self.mas_centerY);
|
||||||
|
make.height.mas_equalTo(36);
|
||||||
|
}];
|
||||||
|
|
||||||
|
// 绑定系统输入法切换事件
|
||||||
|
[self.globeButtonInternal removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
|
||||||
|
if (needSwitchKey && ivc) {
|
||||||
|
SEL sel = NSSelectorFromString(@"handleInputModeListFromView:withEvent:");
|
||||||
|
if ([ivc respondsToSelector:sel]) {
|
||||||
|
[self.globeButtonInternal addTarget:ivc action:sel forControlEvents:UIControlEventAllTouchEvents];
|
||||||
|
} else {
|
||||||
|
[self.globeButtonInternal addTarget:ivc action:@selector(advanceToNextInputMode) forControlEvents:UIControlEventTouchUpInside];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)didMoveToWindow {
|
||||||
|
[super didMoveToWindow];
|
||||||
|
[self kb_refreshGlobeVisibility];
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -6,11 +6,13 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#import "KBToolBar.h"
|
#import "KBToolBar.h"
|
||||||
|
#import "KBResponderUtils.h" // 查找 UIInputViewController,用于系统切换输入法
|
||||||
|
|
||||||
@interface KBToolBar ()
|
@interface KBToolBar ()
|
||||||
@property (nonatomic, strong) UIView *leftContainer;
|
@property (nonatomic, strong) UIView *leftContainer;
|
||||||
@property (nonatomic, strong) NSArray<UIButton *> *leftButtonsInternal;
|
@property (nonatomic, strong) NSArray<UIButton *> *leftButtonsInternal;
|
||||||
@property (nonatomic, strong) UIButton *settingsButtonInternal;
|
@property (nonatomic, strong) UIButton *settingsButtonInternal;
|
||||||
|
@property (nonatomic, strong) UIButton *globeButtonInternal; // 可选:系统“切换输入法”键
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation KBToolBar
|
@implementation KBToolBar
|
||||||
@@ -50,6 +52,7 @@
|
|||||||
- (void)setupUI {
|
- (void)setupUI {
|
||||||
[self addSubview:self.leftContainer];
|
[self addSubview:self.leftContainer];
|
||||||
[self addSubview:self.settingsButtonInternal];
|
[self addSubview:self.settingsButtonInternal];
|
||||||
|
[self addSubview:self.globeButtonInternal];
|
||||||
|
|
||||||
// 右侧设置按钮
|
// 右侧设置按钮
|
||||||
[self.settingsButtonInternal mas_makeConstraints:^(MASConstraintMaker *make) {
|
[self.settingsButtonInternal mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||||
@@ -58,9 +61,16 @@
|
|||||||
make.width.height.mas_equalTo(32);
|
make.width.height.mas_equalTo(32);
|
||||||
}];
|
}];
|
||||||
|
|
||||||
|
// 左侧地球键(按需显示)
|
||||||
|
[self.globeButtonInternal mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||||
|
make.left.equalTo(self.mas_left).offset(12);
|
||||||
|
make.centerY.equalTo(self.mas_centerY);
|
||||||
|
make.width.height.mas_equalTo(32);
|
||||||
|
}];
|
||||||
|
|
||||||
// 左侧容器占用剩余空间
|
// 左侧容器占用剩余空间
|
||||||
[self.leftContainer mas_makeConstraints:^(MASConstraintMaker *make) {
|
[self.leftContainer mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||||
make.left.equalTo(self.mas_left).offset(12);
|
make.left.equalTo(self.globeButtonInternal.mas_right).offset(8);
|
||||||
make.right.equalTo(self.settingsButtonInternal.mas_left).offset(-12);
|
make.right.equalTo(self.settingsButtonInternal.mas_left).offset(-12);
|
||||||
make.centerY.equalTo(self.mas_centerY);
|
make.centerY.equalTo(self.mas_centerY);
|
||||||
make.height.mas_equalTo(32);
|
make.height.mas_equalTo(32);
|
||||||
@@ -89,6 +99,9 @@
|
|||||||
make.right.equalTo(self.leftContainer.mas_right);
|
make.right.equalTo(self.leftContainer.mas_right);
|
||||||
}];
|
}];
|
||||||
self.leftButtonsInternal = buttons.copy;
|
self.leftButtonsInternal = buttons.copy;
|
||||||
|
|
||||||
|
// 初始刷新地球键的可见性与事件绑定
|
||||||
|
[self kb_refreshGlobeVisibility];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (UIButton *)buildActionButtonAtIndex:(NSInteger)idx {
|
- (UIButton *)buildActionButtonAtIndex:(NSInteger)idx {
|
||||||
@@ -142,4 +155,59 @@
|
|||||||
return _settingsButtonInternal;
|
return _settingsButtonInternal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (UIButton *)globeButtonInternal {
|
||||||
|
if (!_globeButtonInternal) {
|
||||||
|
_globeButtonInternal = [UIButton buttonWithType:UIButtonTypeSystem];
|
||||||
|
_globeButtonInternal.layer.cornerRadius = 16;
|
||||||
|
_globeButtonInternal.layer.masksToBounds = YES;
|
||||||
|
_globeButtonInternal.backgroundColor = [UIColor colorWithWhite:1 alpha:0.9];
|
||||||
|
[_globeButtonInternal setTitle:@"🌐" forState:UIControlStateNormal];
|
||||||
|
[_globeButtonInternal setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
|
||||||
|
}
|
||||||
|
return _globeButtonInternal;
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma mark - Globe (Input Mode Switch)
|
||||||
|
|
||||||
|
// 根据宿主是否已提供系统切换键,决定是否显示地球按钮;并绑定系统事件。
|
||||||
|
- (void)kb_refreshGlobeVisibility {
|
||||||
|
UIInputViewController *ivc = KBFindInputViewController(self);
|
||||||
|
BOOL needSwitchKey = YES;
|
||||||
|
if (ivc && [ivc respondsToSelector:@selector(needsInputModeSwitchKey)]) {
|
||||||
|
needSwitchKey = ivc.needsInputModeSwitchKey; // YES 表示自定义键盘需要提供切换键
|
||||||
|
}
|
||||||
|
|
||||||
|
self.globeButtonInternal.hidden = !needSwitchKey;
|
||||||
|
|
||||||
|
// 重新调整 leftContainer 的左约束:若不需要地球键,则贴左边距 12
|
||||||
|
[self.leftContainer mas_remakeConstraints:^(MASConstraintMaker *make) {
|
||||||
|
if (needSwitchKey) {
|
||||||
|
make.left.equalTo(self.globeButtonInternal.mas_right).offset(8);
|
||||||
|
} else {
|
||||||
|
make.left.equalTo(self.mas_left).offset(12);
|
||||||
|
}
|
||||||
|
make.right.equalTo(self.settingsButtonInternal.mas_left).offset(-12);
|
||||||
|
make.centerY.equalTo(self.mas_centerY);
|
||||||
|
make.height.mas_equalTo(32);
|
||||||
|
}];
|
||||||
|
|
||||||
|
// 绑定系统提供的输入法切换处理(点按切换、长按弹出列表)
|
||||||
|
// 仅在需要时绑定,避免多余的事件转发
|
||||||
|
[self.globeButtonInternal removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
|
||||||
|
if (needSwitchKey && ivc) {
|
||||||
|
SEL sel = NSSelectorFromString(@"handleInputModeListFromView:withEvent:");
|
||||||
|
if ([ivc respondsToSelector:sel]) {
|
||||||
|
[self.globeButtonInternal addTarget:ivc action:sel forControlEvents:UIControlEventAllTouchEvents];
|
||||||
|
} else {
|
||||||
|
// 回退:至少在点按时切换
|
||||||
|
[self.globeButtonInternal addTarget:ivc action:@selector(advanceToNextInputMode) forControlEvents:UIControlEventTouchUpInside];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)didMoveToWindow {
|
||||||
|
[super didMoveToWindow];
|
||||||
|
[self kb_refreshGlobeVisibility];
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
Reference in New Issue
Block a user