【问题标题】:done button is not visible in number pad iOS 9 issue完成按钮在数字键盘 iOS 9 问题中不可见
【发布时间】:2015-10-14 13:21:17
【问题描述】:

此代码在 ios 6、7、8 中运行,但在 ios 9 中调用了这个 all 方法,但它不可见。在数字键盘上。这是我的代码。

#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

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

【问题讨论】:

    标签: ios9 numpad


    【解决方案1】:

    改变

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

    收件人:

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

    【讨论】:

    • 是的,这是有效的,我在屏幕上显示按钮,但我没有处理此按钮的事件。使用相同的代码修改建议的更改。
    【解决方案2】:

    好的,这里有一个简单的修复方法,可以根据您的问题在 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' 选择器方法以执行您想要的任何行为,例如在数字键盘或默认键盘之间交换或切换键盘、验证应用程序等。 你应该是金色的!

    【讨论】:

      【解决方案3】:

      首先我们声明一个新变量:

      @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];
          }
      }
      

      keyboardDidShowtextFieldDidBeginEditing方法中显示按钮:

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

      keyboardWillHidetextFieldDidEndEditing 方法中的删除按钮:

          [self.doneButton removeFromSuperview];
      

      这适用于 iOS8iOS9

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-09
        • 1970-01-01
        • 2012-04-19
        • 2015-12-28
        • 2013-12-10
        • 1970-01-01
        • 2022-11-01
        • 2015-01-03
        相关资源
        最近更新 更多