iOS键盘上方移动视图左侧黑色空间

时间:2015-04-10 01:06:42

标签: ios objective-c

我正在使用UIView,视图底部有一个按钮。 当键盘显示/更改高度时,我想在键盘正上方移动按钮。

我使用的代码是:

- (void)keyboardFrameDidChange:(NSNotification *)notification
{
    CGRect keyboardEndFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect keyboardBeginFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];

    CGRect newFrame = self.view.frame;
    CGRect keyboardFrameEnd = [self.view convertRect:keyboardEndFrame toView:nil];
    CGRect keyboardFrameBegin = [self.view convertRect:keyboardBeginFrame toView:nil];

    newFrame.origin.y -= (keyboardFrameBegin.origin.y - keyboardFrameEnd.origin.y);
    self.view.frame = newFrame;
}

并在viewDidLoad中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameDidChange:) name:UIKeyboardDidChangeFrameNotification object:nil];

此代码适用于iPhone6,但如果它在iPhone 5中并使用中文键盘,则会出现黑色空间: image

0 个答案:

没有答案