因此,默认情况下,Numpad键盘没有"完成"或"下一步"按钮,所以我想添加一个.在iOS6及更低版本中,有一些在键盘上添加按钮的技巧,但在iOS7中似乎不起作用.

首先,我订阅显示通知的键盘

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

然后,当键盘出现时,我try 添加一个按钮:

- (void)keyboardWillShow:(NSNotification *)note 
{
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeSystem];
    doneButton.frame = CGRectMake(0, 50, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setTitle:@"Done" forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(dismissKeyboard) forControlEvents:UIControlEventTouchUpInside];

    // locate keyboard view
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;
    for(int i=0; i<[tempWindow.subviews count]; i++) 
    {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard view found; add the custom button to it
        if([[keyboard description] hasPrefix:@"UIKeyboard"] == YES)
        [keyboard addSubview:doneButton];
    }
}

但是for循环不会运行,因为它找不到任何子视图.有什么建议吗?我找不到任何针对iOS7的解决方案,那么有没有其他我应该这样做的方式呢?

编辑:感谢大家对工具栏的建议,但我不想走这条路,因为我的空间太小了(而且有点难看).

推荐答案

这是在iOS7数字键盘中投影完成按钮的简单方法.在UITextField的下面的委托方法中,添加键盘显示的通知.

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
}

现在实现方法keyboardWillShow,如下所示.在这里,我们需要特别注意iOS7.

- (void)keyboardWillShow:(NSNotification *)note {
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setImage:[UIImage imageNamed:@"doneButtonNormal.png"] forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"doneButtonPressed.png"] forState:UIControlStateHighlighted];
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
    
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIView *keyboardView = [[[[[UIApplication sharedApplication] windows] lastObject] subviews] firstObject];
            [doneButton setFrame:CGRectMake(0, keyboardView.frame.size.height - 53, 106, 53)];
            [keyboardView addSubview:doneButton];
            [keyboardView bringSubviewToFront:doneButton];
            
            [UIView animateWithDuration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]-.02
                                  delay:.0
                                options:[[note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]
                             animations:^{
                                 self.view.frame = CGRectOffset(self.view.frame, 0, 0);
                             } completion:nil];
        });
    } else {
        // locate keyboard view
        dispatch_async(dispatch_get_main_queue(), ^{
            UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
            UIView* keyboard;
            for(int i=0; i<[tempWindow.subviews count]; i++) {
                keyboard = [tempWindow.subviews objectAtIndex:i];
                // keyboard view found; add the custom button to it
                if([[keyboard description] hasPrefix:@"UIKeyboard"] == YES)
                    [keyboard addSubview:doneButton];
            }
        });
    }
}

现在将此宏添加到适当的标头以检测SYSTEM_VERSION

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

Ios相关问答推荐

自定义alert 未执行任何操作

构建iOS项目失败.我们运行了";xcodeBuild";命令,但它退出并返回错误代码65-expo reaction ative

我如何确保用户继续他们在应用程序中停止的地方?

SwiftUI@Observable不跟踪父类属性中的更改以更新视图

带有故事板的Xcode 15 UIKit视图可以与#Preview宏一起使用吗?

swift|在具有自定义单元格的多个TableView中没有任何内容

Xcode 15中的版本控制发生了什么?

RFID scanner 的蓝牙服务 UUID?

使用异步重载实现Swift协议一致性

比较 `某个协议` 实例时出现编译错误

我需要二进制文件来进行 App Store 应用程序传输吗?

在 SwiftUI 中重构提取到子视图的 GeometryReader 代码

过渡动画在 iOS16 中不起作用,但在 iOS15 中起作用

有没有办法访问自动生成的 Codable 一致性的编码键?

在 SwiftUI 中打开 PDF

使用 convertPoint 获取父 UIView 内的相对位置

什么是伞头?

在 Objective-C 中将所有文本转换为小写

如何删除 Swift 数组中的所有 nil 元素?

Swift 中的日期到毫秒和回溯到日期