Frage

Der folgende Code (Entschuldigung für die Länge) zeigt ein seltsames Verhalten unter iOS 4.3 (vielleicht auch andere Version). In diesem Beispiel gibt es drei UITextFields, die drei Tastaturen in unterschiedlicher Größe haben. Wenn Sie mit der Bearbeitung eines Textfelds beginnen und dann "return" die Tastatur entlassen, wird jedes Mal, wenn die Tastaturgröße korrekt zurückgegeben wird UIKeyboardWillShowNotification und UIKeyboardDidShowNotification Verwendung UIKeyboardFrameBeginUserInfoKey.

siehe unten:

- (void) keyboardWillShowNotification:(NSNotification *)aNotification

und

- (void) keyboardDidShowNotification:(NSNotification *)aNotification

Beachten Sie, dass dies das erwartete Verhalten ist.

action                 reported keyboard size  expected keyboard size  
---------------------  ----------------------  ----------------------
touch one    & return  100                     100
touch two    & return  200                     200
touch normal & return  216                     216
n            & return  keyboard size(n)        keyboard size(n)

Das unerwartete Verhalten ist, wenn Sie mit der Bearbeitung eines Textfelds mit der Bearbeitung eines Textfelds der ersten Tastatur angegeben werden (erwartet). Wenn Sie das zweite Textfeld berühren (ohne die Rückkehr zu berühren), wird die Größe der ersten Tastatur (unerwartet) anstelle der Größe der zweiten gemeldet. Wenn Sie das dritte Textfeld berühren (ohne Rückkehr), wird die Größe der zweiten Tastaturgröße (unerwartet) anstelle der Größe der dritten gemeldet. Für das zweite bis n -te Mal scheint es, als würde es die Größe der vorherigen Tastatur nicht die angezeigt, die angezeigt wird.

action        reported keyboard size  expected keyboard size  
------------  ----------------------  ----------------------
touch one     100                     100
touch two     100                     200
touch normal  200                     216
touch one     216                     100
n             keyboard size(n-1)      keyboard size(n)

Bevor ich einen Fehlerbericht sende, möchte ich nur sicherstellen, dass ich nichts überprüft habe.

Zu Ihrer Information Ich habe mich dabei gestürzt, während ich versuchte, das Richtige zu tun (mit Verwendung UIKeyboardWillShowNotification oder UIKeyboardDidShowNotification und UIKeyboardFrameBeginUserInfoKey Um die Größe der Tastatur zu erhalten) beim Verlagerungen einer Ansicht, so dass ein Textfeld, das durch eine Tastatur verdeckt wäre, sichtbar ist. Bezug:

Wie kann ein Uitextfield aufsteigen, wenn die Tastatur vorhanden ist?

iOS -Bibliothek: Text-, Web- und Bearbeitungsprogrammierhandbuch für iOS -> die Tastatur verwalten

iOS -Bibliothek: Programmierhandbuch für scrollenansicht für iOS -> Erstellen und Konfigurieren von Bildlaufansichten

Bugvc.h

#import <UIKit/UIKit.h>

@interface BugVC : UIViewController <UITextFieldDelegate> {
    UITextField *oneTF;
    UITextField *twoTF;
    UITextField *normalTF;
    UILabel *keyboardWillShowNotificationL;
    UILabel *keyboardDidShowNotificationL;
}

- (void) oneReturnTouchDown:(id)sender;
- (void) twoReturnTouchDown:(id)sneder;
- (void) keyboardWillShowNotification:(NSNotification *)aNotification;
- (void) keyboardDidShowNotification:(NSNotification *)aNotification;

@end

Bugvc.m

#import "BugVC.h"

@implementation BugVC

- (id) init
{
    if ( !(self = [super init]) )
    {
        return self;
    }

    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    // One text field with 100 height keyboard
    oneTF = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 300, 30)];
    oneTF.borderStyle = UITextBorderStyleRoundedRect;
    oneTF.text = @"100";
    oneTF.delegate = self;
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // Custom input view for the above text field
    UIView *oneInputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
    oneInputView.backgroundColor = [UIColor redColor];
    UIButton *oneReturnB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    oneReturnB.frame = CGRectMake(10, 10, 300, 30);
    [oneReturnB setTitle:@"return" forState:UIControlStateNormal];
    [oneReturnB addTarget:self
                   action:@selector(oneReturnTouchDown:)
         forControlEvents:UIControlEventTouchDown];
    [oneInputView addSubview:oneReturnB];
    oneTF.inputView = oneInputView;
    [oneInputView release];
    [self.view addSubview:oneTF];

    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    // Two text field with 200 height keyboard
    twoTF = [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 300, 30)];
    twoTF.borderStyle = UITextBorderStyleRoundedRect;
    twoTF.text = @"200";
    twoTF.delegate = self;
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // Custom input view for the above text field
    UIView *twoInputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
    twoInputView.backgroundColor = [UIColor blueColor];
    UIButton *twoReturnB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    twoReturnB.frame = CGRectMake(10, 10, 300, 30);
    [twoReturnB setTitle:@"return" forState:UIControlStateNormal];
    [twoReturnB addTarget:self
                   action:@selector(twoReturnTouchDown:)
         forControlEvents:UIControlEventTouchDown];
    [twoInputView addSubview:twoReturnB];
    twoTF.inputView = twoInputView;
    [twoInputView release];
    [self.view addSubview:twoTF];

    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    // normal text field with normal keyboard (216 height keyboard)
    normalTF = [[UITextField alloc] initWithFrame:CGRectMake(10, 90, 300, 30)];
    normalTF.borderStyle = UITextBorderStyleRoundedRect;
    normalTF.text = @"normal";
    normalTF.delegate = self;
    [self.view addSubview:normalTF];

    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    // Label that displays the keyboard height from keyboardWillShowNotification
    keyboardWillShowNotificationL = [[UILabel alloc] initWithFrame:CGRectMake(10, 130, 300, 30)];
    keyboardWillShowNotificationL.font = [UIFont systemFontOfSize:14];
    keyboardWillShowNotificationL.text = @"keyboardWillShowNotification kbHeight:";
    [self.view addSubview:keyboardWillShowNotificationL];

    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    // Label that displays the keyboard height from keyboardDidShowNotification
    keyboardDidShowNotificationL = [[UILabel alloc] initWithFrame:CGRectMake(10, 170, 300, 30)];
    keyboardDidShowNotificationL.font = [UIFont systemFontOfSize:14];
    keyboardDidShowNotificationL.text = @"keyboardDidShowNotification kbHeight:";
    [self.view addSubview:keyboardDidShowNotificationL];

    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    // Register for keyboard notifications.
    [[NSNotificationCenter defaultCenter]
     addObserver:self
        selector:@selector(keyboardWillShowNotification:)
            name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter]
     addObserver:self
        selector:@selector(keyboardDidShowNotification:)
            name:UIKeyboardDidShowNotification object:nil];

    return self;
}

- (void) dealloc
{
    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    // Deregister for keyboard notifications
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
               name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter]
     removeObserver:self
               name:UIKeyboardDidShowNotification object:nil];

    [oneTF release];
    [twoTF release];
    [normalTF release];
    [keyboardWillShowNotificationL release];
    [keyboardDidShowNotificationL release];

    [super dealloc];
}

- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];

    return YES;
}

- (void) oneReturnTouchDown:(id)sender
{
    [oneTF.delegate textFieldShouldReturn:oneTF];
}

- (void) twoReturnTouchDown:(id)sneder
{
    [twoTF.delegate textFieldShouldReturn:twoTF];
}

- (void) keyboardWillShowNotification:(NSNotification *)aNotification
{
    NSDictionary *info = [aNotification userInfo];
    CGFloat kbHeight =
        [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;

    NSString *string = [[NSString alloc] initWithFormat:@"keyboardWillShowNotification kbHeight: %.2f", kbHeight];
    NSLog(@"%@", string);
    keyboardWillShowNotificationL.text = string;
    [string release];
}

- (void) keyboardDidShowNotification:(NSNotification *)aNotification
{
    NSDictionary *info = [aNotification userInfo];
    CGFloat kbHeight =
        [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;

    NSString *string = [[NSString alloc] initWithFormat:@"keyboardDidShowNotification kbHeight: %.2f", kbHeight];
    NSLog(@"%@", string);
    keyboardDidShowNotificationL.text = string;
    [string release];
}

@end
War es hilfreich?

Lösung

Wie in diese Frage, das start frame (gekeutet von UIKeyboardFrameBeginUserInfoKey) dort ist die Tastatur am Anfang der Animation. UIKeyboardFrameEndUserInfoKey sollte dir die bringen end frame stattdessen. Vermutlich unterscheidet sich die Größe auch zwischen den Frames.

Schlüsselreferenz: http://developer.apple.com/library/ios/#documentation/uikit/reference/uiwindow_class/uiwindowclassreference/uiwindowclassreference.html#//apple_ref/doc/constant_group/keyboard_notification_user_info_keys

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top