试图移动视图向上的键盘时,显示

问题描述:

我试图把我的看法了,当键盘显示(它覆盖的数据我希望用户看到,而他的打字,我使用此代码:试图移动视图向上的键盘时,显示

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

我发现here。我试图弄清楚为什么我的观点没有提高。键盘显示并且keyboardSizeChanged被激活,但视图不移动。我打开了一个新项目,并将KBKeyboardHandler和委托文件复制到它,实现了代码,并且在新项目中它工作正常,所以我知道这是我原始项目中的某些事情搞砸了。任何想法可能是什么?

如果你不使用一个UITableView使用UIScrollView的为您的元素的父视图,并添加下面 代码根据需要调整,

您需要NSObjects的NSArray的,我打电话给我scrollToObjects和viewDidLoad中添加数组中需要滚动的元素,以便在调用keyboardWasShown时可以检查当前选定的元素是否应滚动到。

我用过自我。activeField用于存储我当前选定的元素,当元素触发事件时(事件需要是第一个被调用的并且始终被调用的元素),它将被设置为

然后我检查scrollToObjects是否包含activeField,不滚动的UIScrollView起来,然后回落时activeField resignsFirstResponder

// 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; 
} 

我会检查你的计算三角洲。

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

我的猜测是oldFrameself.frame可能具有相同的尺寸。

我在想,如果还有其他东西可能会错过,比如正确地将视图链接到这些对象或界面编辑器中的某些东西?

问题出在keyboardSizeChanged上: - 你想调整的是frame.origin.y,而不是frame.size.height。至少这就是想要它做的。

您还可以尝试其他库,它被称为TPKeyboardAvoiding。 (通过身份检查在厦门国际银行仍然 )

  1. 添加一个UIScrollView到您的视图控制器的XIB
  2. 的滚动视图类为TPKeyboardAvoidingScrollView
  3. :这是伟大的工作,而且非常易于安装
  4. 将所有控件放置在该滚动视图中

它还会自动挂上键盘上的“下一步”按钮以切换文本字段。