4

此代码在 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

然后你需要进入背景并进入前台,它会在几秒钟内可见,而不是隐藏。请帮我。谢谢你

4

3 回答 3

1

改变

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

至 :

UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] lastObject];
于 2015-11-09T07:22:36.580 回答
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比在ortextFieldDidEndEditing方法中删除按钮:

    [self.doneButton removeFromSuperview];

这适用于iOS8iOS9

于 2016-09-09T09:55:00.133 回答
0

好的,这是一个简单的修复方法,用于在 iOS 9 和 iOS 8 及更低版本的应用程序中显示“完成”按钮来解决您的问题。在运行应用程序并通过“视图的层次结构”查看它(即,当应用程序在设备上运行并检查您在 Storyboard 中的视图时,单击“调试区域”标题栏中的“查看层次结构”图标)后,可以观察到,显示了键盘与 iOS 8 及更低版本相比,在 iOS 9 的不同窗口上,必须加以考虑。

首先,我们声明一个全局属性,UIButton 类型的“buttonDone”,并在我们的实现文件中使用它,如下所示:

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

}

实现' doneButton '选择器方法来执行你想要的任何行为,比如在数字键盘或默认值之间交换或切换键盘,验证应用程序等。你应该是金子!

于 2015-11-27T21:02:46.397 回答