3

キーボードが表示されているときにビューを押し上げようとしています(ユーザーが入力しているときに表示したいデータをオーバーレイします。次のコードを使用しています:

KBKeyboardHandler.h :

@protocol KBKeyboardHandlerDelegate;

@interface KBKeyboardHandler : NSObject

- (id)init;

// Put 'weak' instead of 'assign' if you use ARC
@property(nonatomic, assign) id<KBKeyboardHandlerDelegate> delegate; 
@property(nonatomic) CGRect frame;

@end

KBKeyboardHandler.m :

#import "KBKeyboardHandler.h"
#import "KBKeyboardHandlerDelegate.h"

@implementation KBKeyboardHandler

- (id)init
{
    self = [super init];
    if (self)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
    }

    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

@synthesize delegate;
@synthesize frame;

- (void)keyboardWillShow:(NSNotification *)notification
{
    CGRect oldFrame = self.frame;    
    [self retrieveFrameFromNotification:notification];

    if (oldFrame.size.height != self.frame.size.height)
    {
        CGSize delta = CGSizeMake(self.frame.size.width - oldFrame.size.width,
                                  self.frame.size.height - oldFrame.size.height);
        if (self.delegate)
            [self notifySizeChanged:delta notification:notification];
    }
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    if (self.frame.size.height > 0.0)
    {
        [self retrieveFrameFromNotification:notification];
        CGSize delta = CGSizeMake(-self.frame.size.width, -self.frame.size.height);

        if (self.delegate)
            [self notifySizeChanged:delta notification:notification];
    }

    self.frame = CGRectZero;
}

- (void)retrieveFrameFromNotification:(NSNotification *)notification
{
    CGRect keyboardRect;
    [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardRect];
    self.frame = [[UIApplication sharedApplication].keyWindow.rootViewController.view convertRect:keyboardRect fromView:nil];
}

- (void)notifySizeChanged:(CGSize)delta notification:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];

    UIViewAnimationCurve curve;
    [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&curve];

    NSTimeInterval duration;
    [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&duration];

    void (^action)(void) = ^{
        [self.delegate keyboardSizeChanged:delta];
    };

    [UIView animateWithDuration:duration
                          delay:0.0
                        options:curve
                     animations:action
                     completion:nil];    
}

@end

KBKeyboardHandlerDelegate.h :

@protocol KBKeyboardHandlerDelegate

- (void)keyboardSizeChanged:(CGSize)delta;

@end

サンプルMyViewController.h :

@interface MyViewController : UIViewController<KBKeyboardHandlerDelegate>
...
@end

サンプルMyViewController.m :

@implementation MyViewController
{
    KBKeyboardHandler *keyboard;
}

- (void)dealloc
{
    keyboard.delegate = nil;
    [keyboard release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    keyboard = [[KBKeyboardHandler alloc] init];
    keyboard.delegate = self;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    keyboard.delegate = nil;
    [keyboard release];
    keyboard = nil;
}

- (void)keyboardSizeChanged:(CGSize)delta
{
    // Resize / reposition your views here. All actions performed here 
    // will appear animated.
    // delta is the difference between the previous size of the keyboard 
    // and the new one.
    // For instance when the keyboard is shown, 
    // delta may has width=768, height=264,
    // when the keyboard is hidden: width=-768, height=-264.
    // Use keyboard.frame.size to get the real keyboard size.

    // Sample:
    CGRect frame = self.view.frame;
    frame.size.height -= delta.height;
    self.view.frame = frame;
}

ここで見つけたもの。なぜ私の見解が上がらないのかを理解しようとしています。キーボードが表示keyboardSizeChangedされ、起動されますが、ビューは移動しません。私は新しいプロジェクトを開き、KBKeyboardHandler とデリゲート ファイルをそれにコピーし、コードを実装しました。新しいプロジェクトでは正常に動作するので、元のプロジェクトの何かが台無しになっていることがわかります。それが何であるか考えていますか?

4

4 に答える 4

1

UITableViewを使用していない場合は、要素の親ビューとしてUIScrollViewを使用し、必要に応じてAdjustの下にコードを追加します。

NSObjectsのNSArrayが必要になります。私はscrollToObjectsを呼び出し、viewDidLoadでスクロールが必要な要素を配列に追加して、keyboardWasShownが呼び出されたときに、現在選択されている要素をスクロールする必要があるかどうかを確認できるようにします。

self.activeFieldを使用して、要素がイベントをトリガーしたときに設定される現在選択されている要素を格納しました(イベントは最初に呼び出されるものの1つである必要があり、常に呼び出されます)

次に、scrollToObjectsにactiveFieldが含まれているかどうか、およびactiveFieldがFirstResponderを辞任したときにUIScrollViewを上にスクロールしてから下にスクロールするかどうかを確認します。

// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    // Wouldn't go true so used an array that contains text fields that need to be scrolled   to
    //    if (!CGRectContainsPoint(aRect, self.activeField.frame.origin) ) {
    //        CGPoint scrollPoint = CGPointMake(0.0, self.activeField.frame.origin.y-    kbSize.height);
    //        [self.scrollView setContentOffset:scrollPoint animated:YES];
    //    }
    if ([self.scrollToObjects containsObject:self.activeField]) {
        CGPoint scrollPoint = CGPointMake(0.0, self.activeField.frame.origin.y+    (self.activeField.frame.size.height*2)-kbSize.height);
        [self.scrollView setContentOffset:scrollPoint animated:YES];
    }
}

// Called when the UIKeyboardWillHideNotification is sent 
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{ 
    // self.scrollView.contentInset = UIEdgeInsetsZero;
    [self.scrollView setContentOffset:CGPointMake(0.0, 0.0) animated:YES];
    self.scrollView.scrollIndicatorInsets = UIEdgeInsetsZero;
}

より簡単な解決策は、以下のコードまたはオブジェクトのアクティブ化と非アクティブ化に類似したものを使用して、以下のUITextFieldDelegateメソッドを処理することです。

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGPoint scrollPoint = CGPointMake(0.0, textField.frame.origin.y);
    [self.theScrollView setContentOffset:scrollPoint animated:YES];
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    [self.theScrollView setContentOffset:CGPointMake(0.0, 0.0) animated:YES];
    self.theScrollView.scrollIndicatorInsets = UIEdgeInsetsZero;
}
于 2013-01-18T17:28:29.313 に答える
0

デルタの計算を確認します。

CGSize delta = CGSizeMake(self.frame.size.width - oldFrame.size.width,
                              self.frame.size.height - oldFrame.size.height);

私の推測ではoldFrameself.frame同じ寸法である可能性があります。

ビューをこれらのオブジェクトに適切にリンクしたり、インターフェースエディターで何かをしたりするなど、見逃した可能性のあるものが他にあるかどうかを考えようとしています。

于 2013-01-18T13:31:49.553 に答える