iOS 数字键盘,能够打开文本键盘

标签 ios swift keyboard numpad

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

enter image description here

另请参阅此视频:https://youtu.be/iU_jocny3N0

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

我找到的唯一解决方案是像这里描述的那样向键盘添加一个 subview :

Adding Done Button to Only Number Pad Keyboard on iPhone

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

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

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

最佳答案

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

键盘也是一个简单的UIView,其中包含控件

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

- (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;
}

关于iOS 数字键盘,能够打开文本键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46585218/

相关文章:

ios - "sudo sysctl net.inet.ip.fw.enable=1"不适用于 macOS Sierra

ios - 将 subview 添加到 UICollectionViewCell 子类?

iphone - 来自 NSString 的 NSDate,如 HH :mm

ios - Apple - 应用内购买 : How to make sure the user is authorised to use Free trial subscription only one time?

ios - 将值插入字典数组

iphone - 键盘出现时 Xcode/iOS5 : Move UIView up,

ios - 社交网络应用程序在返回线上出现错误

ios - 如何获取已通过 snapkit 设置的 UIView 的框架

ios - 如何根据 iPhone 向上移动文本字段。 swift 大小

java - 如何防止用户从 android studio 中的键盘输入特定字符?