【问题标题】:iOS number pad keyboard with ability to switch on text keyboardiOS 数字键盘,能够打开文本键盘
【发布时间】:2017-10-05 12:06:15
【问题描述】:

我有一个文本字段,我想在用户开始输入时使用这样的键盘:

也请观看此视频:https://youtu.be/iU_jocny3N0

正如您在此视频中看到的,有一个“ABC”键可帮助用户从数字键盘切换到文本。并且当在文本中按“123”时,键盘会从文本切换到数字键盘。我想知道他们是怎么做到的?

我发现的唯一解决方案是向键盘添加一个子视图,如下所述:

Adding Done Button to Only Number Pad Keyboard on iPhone

但是当用户使用自定义键盘时,这种方式可能不起作用。也不适用于从文本切换到数字键盘。

或者作为我知道accessoryInputView 的另一种解决方案,但这与视频不同。它在键盘上方添加了一个工具栏。

有人知道此视频中使用的解决方案吗?

【问题讨论】:

  • 在我的旧项目中,我在 Keyboard 上添加了 My Custom 按钮。但它是 Objc 代码
  • 你是通过向uiwindow添加子视图来添加这个的吗?
  • 不,在键盘上!!!
  • 可以分享代码吗?
  • 可能你也可以看看自定义键盘扩展developer.apple.com/library/content/documentation/General/…

标签: ios swift keyboard numpad


【解决方案1】:

我在键盘上添加了逗号按钮,

Keyboard 也是一个简单的 UIView,其中包含 Controls

注意:这是旧代码在我的旧项目中运行 未在新项目中测试

- (void) keyboardWillShow:(NSNotification *)note {
    // create custom button

    dispatch_async(dispatch_get_main_queue(), ^{
        // Some code
        UITextField *txt = (UITextField *)[self.view findFirstResponder];
        if (txt.keyboardType == UIKeyboardTypeDecimalPad) {
            UIButton * btnComma = [UIButton buttonWithType:UIButtonTypeCustom];
            [btnComma setTag:15000];

            UIView* keyboard = [self findKeyboard];

//            btnComma.frame = CGRectMake(0, 162, 126, 54);

            btnComma.frame = [self findKeySizeForView:keyboard];







            if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
                [btnComma setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 20, 0)];
            }

            [btnComma setBackgroundColor:[UIColor colorWithHexString:@"CBD0D6"]];
            btnComma.adjustsImageWhenHighlighted = NO;
            [btnComma setTitle:@"." forState:UIControlStateNormal];
            [btnComma setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btnComma.titleLabel setFont:[UIFont systemFontOfSize:35.0f]];
            [btnComma addTarget:self action:@selector(commaBtnTapped) forControlEvents:UIControlEventTouchUpInside];

            [keyboard addSubview:btnComma];
            btnComma = nil;
        }
    });
}
- (UIView *) viewWithPrefix:(NSString *)prefix inView:(UIView *)view {
    for (UIView *subview in view.subviews) {
        if ([[subview description] hasPrefix:prefix]) {
            return subview;
        }
    }
    return nil;
}

这个从UIWindow中寻找键盘的方法

- (UIView *) findKeyboard {
    for (UIWindow* window in [UIApplication sharedApplication].windows) {
        UIView *inputSetContainer = [self viewWithPrefix:@"<UIInputSetContainerView" inView:window];
        if (inputSetContainer) {
            UIView *inputSetHost = [self viewWithPrefix:@"<UIInputSetHostView" inView:inputSetContainer];
            if (inputSetHost) {
                UIView *kbinputbackdrop = [self viewWithPrefix:@"<_UIKBCompatInput" inView:inputSetHost];
                if (kbinputbackdrop) {
                    UIView *theKeyboard = [self viewWithPrefix:@"<UIKeyboard" inView:kbinputbackdrop];
                    return theKeyboard;
                }
            }
        }
    }

    return nil;
}

和用于查找右下角按钮的大小

- (CGRect ) findKeySizeForView:(UIView *)view {
    if (view != nil) {
        UIView *uiKeyboardImpl = [self viewWithPrefix:@"<UIKeyboardImpl" inView:view];
        if (uiKeyboardImpl != nil) {
            UIView *uiKeyboardLayoutStar = [self viewWithPrefix:@"<UIKeyboardLayoutStar" inView:uiKeyboardImpl];
            if (uiKeyboardLayoutStar != nil) {
                UIView *uiKBKeyplaneView = [self viewWithPrefix:@"<UIKBKeyplaneView" inView:uiKeyboardLayoutStar];
                if (uiKBKeyplaneView != nil) {
                    for (view in [uiKBKeyplaneView subviews]) {
                        CGPoint pointOrigin =  view.layer.frame.origin;
                        if (pointOrigin.x <= 0 && pointOrigin.y == uiKBKeyplaneView.frame.size.height - view.frame.size.height && [[view description] hasPrefix:@"<UIKBKeyView"])
                            return  view.layer.frame;
                    }
                }
            }
        }

    }
    return CGRectZero;
}

【讨论】:

  • 如果用户使用自定义按钮,findKeySizeForView 可能无法正常工作。我对吗?也许是自定义按钮中的右键是必不可少的关键。
  • findKeySizeForView 是一种从键盘获取按钮精确大小的方法!所以btnComma.frame 是从该函数的返回大小分配的
  • 让我试试。我猜代码会起作用,但会担心自定义键盘,但我也会使用自定义键盘对其进行测试。让我一点时间。
  • @HuseinBehboodiRad 祝你好运!!
  • 请您看一下这个视频:youtu.be/iU_jocny3N0 似乎他们没有在键盘上添加子视图。我想知道他们在做什么?
猜你喜欢
  • 1970-01-01
  • 2021-10-25
  • 1970-01-01
  • 2019-09-18
  • 2017-10-02
  • 2021-06-19
  • 1970-01-01
  • 1970-01-01
  • 2016-12-28
相关资源
最近更新 更多