键盘出现时如何推送内容?

时间:2013-11-18 09:40:05

标签: iphone objective-c

我是Qbjectives-C的完全新手。

我之前曾经问过这个问题,但我从那里提供的答案中并不十分理解。

2 个答案:

答案 0 :(得分:-1)

您需要添加通知以捕获键盘状态。在通知中,您可以捕获键盘大小并根据键盘大小移动视图。

#define kKeyboardAnimationDuration 0.3
@interface YourViewController:UIViewController
{
    BOOL keyboardIsShown;
}

然后在您的实施中

- (void)viewDidLoad
{
// register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification 
                                               object:self.view.window];
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillHide:) 
                                                 name:UIKeyboardWillHideNotification 
                                               object:self.view.window];

    keyboardIsShown = NO;
}

- (void)viewDidUnload
{
[super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:UIKeyboardWillShowNotification 
                                                  object:nil]; 
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:UIKeyboardWillHideNotification 
                                                  object:nil]; 
}

#pragma mark - Keyboard Events
- (void)keyboardWillShow:(NSNotification *)n
{
    if (keyboardIsShown) 
    {
        return;
    }

    NSDictionary* userInfo = [n userInfo];
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y-= (keyboardSize.height);

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:kKeyboardAnimationDuration];
    [self.view setFrame:viewFrame];
    [UIView commitAnimations];

    keyboardIsShown = YES;
}

- (void)keyboardWillHide:(NSNotification *)n
{
    NSDictionary* userInfo = [n userInfo];
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y+= (keyboardSize.height);

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:kKeyboardAnimationDuration];
    [self.view setFrame:viewFrame];
    [UIView commitAnimations];

    keyboardIsShown = NO;
}

答案 1 :(得分:-2)

使用滚动视图在键盘出现时向上推送您的内容。

使用此“TPKeyboardAvoidingScrollView”,来自:

https://github.com/michaeltyson/TPKeyboardAvoiding

真的很好。