键盘出现时移动UIView

1 iphone objective-c ios

对不起我的英语.

我试图在行动之前找到它.但问题是ViewController在景观和创造的UIView一半ViewController.在UIViewUITextView.但现在当键盘在键盘ViewController下方向下滚动时出现背景.只看到UIView.如果触摸空间,键盘将消失,背景将恢复.我想只是移动UIView键盘出现时.

非常感谢你.

hch*_*n02 10

试试这个

- (void)viewDidAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)note
{
    CGRect keyboardBounds;
    NSValue *aValue = [note.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];

    [aValue getValue:&keyboardBounds];
    keyboardHeight = keyboardBounds.size.height;
    if (!keyboardIsShowing)
    {
        keyboardIsShowing = YES;
        CGRect frame = view.frame;
        frame.size.height -= 168;

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:0.3f];
        view.frame = frame;
        [UIView commitAnimations];
    }
}

- (void)keyboardWillHide:(NSNotification *)note
{
    CGRect keyboardBounds;
    NSValue *aValue = [note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
    [aValue getValue: &keyboardBounds];

    keyboardHeight = keyboardBounds.size.height;
    if (keyboardIsShowing)
    {
        keyboardIsShowing = NO;
        CGRect frame = view.frame;
        frame.size.height += 168;

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:0.3f];
        view.frame = frame;
        [UIView commitAnimations];

    }
}
Run Code Online (Sandbox Code Playgroud)

  • 您应该在'viewDidAppear'中添加超级调用. (2认同)