done button is not visible in number pad iOS 9 issue

岁酱吖の 提交于 2019-12-04 03:50:15

问题


this code is working in ios 6,7,8 but this all method is called in ios 9 but it is not visible. on number pad. here is my code.

#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

then you need to go background and come on fore ground it will visible for few second than it will go hide. please help me. thank you


回答1:


Change

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

To :

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



回答2:


Okay here's a simple fix for getting 'done' button to show in an app in both iOS 9 and iOS 8 and below taylored to your question. It could be observed after running an app and viewing it via 'View's Hierarchy' (i.e. clicking on the 'View Hierarchy' icon from Debug Area header bar while app is running on device and inspecting your views in Storyboard), that the keyboard is presented on different windows in iOS 9 compared to iOS 8 and below versions and have to be accounted for.

First we declare a global property, 'buttonDone' of type UIButton and use it in our implementation file as below:

#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];

}

Implement the 'doneButton' selector method to perform any behavior you want, like swap or toggle keyboards between numberPads or default, authenticate the app etc. And you should be golden!




回答3:


First of all we are declaring new variable:

@property (strong, nonatomic) UIButton *doneButton;

Call button initialization in 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];
    }
}

Show button in keyboardDidShow or textFieldDidBeginEditingmethod:

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

Than remove button in keyboardWillHide or textFieldDidEndEditing method:

    [self.doneButton removeFromSuperview];

This works on both iOS8 and iOS9.



来源:https://stackoverflow.com/questions/33126504/done-button-is-not-visible-in-number-pad-ios-9-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!