4

このコードは ios 6,7,8 で動作していますが、この all メソッドは 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

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の両方で機能します

于 2016-09-09T09:55:00.133 に答える
0

さて、ここでは、iOS 9 と iOS 8 以下の両方のアプリで「完了」ボタンを表示するための簡単な修正を、質問に合わせて調整します。アプリを実行し、「View's Hierarchy」を介して表示した後 (つまり、アプリがデバイスで実行され、ストーリーボードでビューを検査しているときに、デバッグ エリアのヘッダー バーから [View Hierarchy] アイコンをクリック) すると、キーボードが表示されることがわかります。 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 に答える