此代码在ios 6,7,8中有效,但是这个方法在ios 9中调用但是不可见。在数字垫上。这是我的代码。
#import "ViewController.h"
#define TAG_BUTTON_DONE 67125
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)keyboardDidShow:(NSNotification *)note {
[self addButtonToKeyboard];
}
- (void)addButtonToKeyboard{
//NSLog(@"addButtonToKeyboard");
//jenish
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setTag:TAG_BUTTON_DONE];
//[doneButton setImage:[UIImage imageNamed:@"doneup.png"] forState:UIControlStateNormal];
//[doneButton setImage:[UIImage imageNamed:@"donedown.png"] forState:UIControlStateHighlighted];
[doneButton setTitle:@"Done" forState:UIControlStateNormal];
[doneButton setTintColor:[UIColor blackColor]];
[doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
int windowCount = (int)[[[UIApplication sharedApplication] windows] count];
if (windowCount < 2) {
return;
}
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES){
[keyboard addSubview:doneButton];
}
else if([[keyboard description] hasPrefix:@"<UIInputSetContainerView"] == YES){
for(int j = 0 ; j < [keyboard.subviews count] ; j++) {
UIView* hostkeyboard = [keyboard.subviews objectAtIndex:j];
if([[hostkeyboard description] hasPrefix:@"<UIInputSetHost"] == YES){
[hostkeyboard addSubview:doneButton ];
[hostkeyboard bringSubviewToFront:doneButton];
}
}
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
[keyboard addSubview:doneButton];
});
}
}
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if(touch.phase == UITouchPhaseBegan) {
[self.tf resignFirstResponder];
}
}
@end
然后你需要去背景并且在前面它会显示几秒钟而不是隐藏。请帮我。 谢谢
答案 0 :(得分:1)
更改
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
致:
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] lastObject];
答案 1 :(得分:0)
好的,这是一个简单的解决方案,可以帮助您完成工作。按钮显示在iOS 9和iOS 8及以下版本的应用程序中。 在运行应用程序并通过“查看的层次结构”查看后,可以观察到它。 (即,当应用程序在设备上运行并在Storyboard中检查您的视图时,单击“调试区域”标题栏中的“查看层次结构”图标),键盘将显示在不同的窗口中iOS 9与iOS 8及以下版本相比,必须加以考虑。
首先我们声明一个全局属性,&#39; buttonDone&#39;类型为UIButton并在我们的实现文件中使用它,如下所示:
#import "ViewController.h"
#define TAG_BUTTON_DONE 67125
@interface ViewController ()
@property (nonatomic, strong) UIButton *doneButton;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)keyboardDidShow:(NSNotification *)note {
[self addButtonToKeyboard];
}
- (id)addButtonToKeyboard
{
if (!doneButton)
{
// create custom button
doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setTag:TAG_BUTTON_DONE];
//[doneButton setImage:[UIImage imageNamed:@"doneup.png"] forState:UIControlStateNormal];
//[doneButton setImage:[UIImage imageNamed:@"donedown.png"] forState:UIControlStateHighlighted];
[doneButton setTitle:@"Done" forState:UIControlStateNormal];
[doneButton setTintColor:[UIColor blackColor]];
}
NSArray *windows = [[UIApplication sharedApplication] windows];
//Check to see if running below iOS 9,then return the second window which bears the keyboard
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) {
return windows[windows.count - 2];
}
else {
UIWindow* keyboardWithDoneButtonWindow = [ windows lastObject];
return keyboardWithDoneButtonWindow;
}
[buttonDone addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
}
实施&#39; doneButton &#39;选择器方法来执行您想要的任何行为,例如在numberPads之间交换或切换键盘或默认,对应用程序进行身份验证等。 你应该是金色的!
答案 2 :(得分:0)
首先,我们宣布新的变量:
@property (strong, nonatomic) UIButton *doneButton;
viewDidLoad
中的呼叫按钮初始化:
- (void)setupDoneButton {
if (!self.doneButton) {
self.doneButton = [UIButton buttonWithType:UIButtonTypeSystem];
[self.doneButton addTarget:self action:@selector(tapGestureRecognizerAction) forControlEvents:UIControlEventTouchUpInside];
self.doneButton.adjustsImageWhenHighlighted = NO;
[self.doneButton setTitle:@"DONE" forState:UIControlStateNormal];
[self.doneButton.titleLabel setFont:[UIFont systemFontOfSize:16.0]];
[self.doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
}
}
在keyboardDidShow
或textFieldDidBeginEditing
方法中显示按钮:
- (void)addDoneButtonToKeyboard {
dispatch_async(dispatch_get_main_queue(), ^{
UIWindow *keyboardWindow = [[[UIApplication sharedApplication] windows] lastObject];
CGFloat buttonWidth = CGRectGetWidth(keyboardWindow.frame)/3;
self.doneButton.frame = CGRectMake(0.f, CGRectGetHeight(keyboardWindow.frame) - 53, buttonWidth, 53);
[keyboardWindow addSubview:self.doneButton];
[keyboardWindow bringSubviewToFront:self.doneButton];
});
}
删除keyboardWillHide
或textFieldDidEndEditing
方法中的按钮:
[self.doneButton removeFromSuperview];
适用于 iOS8 和 iOS9 。