用于在Numberkeypad中添加完成按钮的代码
- (void)keyboardWillShow:(NSNotification *)note {
// create custom button
doneButton = [[UIButton alloc]init];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setTitle:@"Done" forState:UIControlStateNormal];
[doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[doneButton addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside];
if (IS_iOS_7_OR_LATER) {
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];
}
});
}
}
我已经编写了在数字小键盘中添加完成按钮的代码,它与设备的默认键盘完美配合使用,如果该设备与第三方键盘一起安装,则会出现问题[快速键应用程序],该方法可以识别第三方键盘来解决此问题
提前致谢!
最佳答案
如果您愿意的话,我知道一种方法,但您却完全不知道。
您不需要任何第三方库来使用此方法
您只需创建UIBarButton并向UIToolbar添加按钮,最后在实现它后添加您创建的工具栏
YourNumberTextField.inputAccessoryView = textFieldToolBar;
这是一个代码
UIToolbar* textFieldToolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
textFieldToolBar.barStyle = UIBarStyleBlackTranslucent;
textFieldToolBar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"Clear All" style:UIBarButtonItemStyleBordered target:self action:@selector("YourClearButtonFunctionName")],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Ok!" style:UIBarButtonItemStyleDone target:self action:@selector("YourOkButtonFunctionName")],
nil];
[textFieldToolBar sizeToFit];
YourNumberTextField.inputAccessoryView = textFieldToolBar;
关于ios - iOS 8 NumberKeyPad引起问题中的完成按钮,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29282091/