ios9 – 完成按钮在数字键盘iOS 9问题中不可见

这段代码在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

然后你需要去背景并且在前面它会显示几秒钟而不是它会隐藏.请帮我.
谢谢

最佳答案 更改

UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];

至 :

UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] lastObject];
点赞