1

我正在 ios 中开发一个聊天应用程序,我有自定义的 tableview 和 UIView,底部有一些 textfield ane 按钮。当 Textfield 被激活时,我想用键盘移动 UIView 和 Tableview。我有这个观察者:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];

然后是keyboardWasShown方法:

- (void)keyboardWasShown:(NSNotification*)aNotification{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
NSNumber *number = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
double duration = [number doubleValue];
    [UIView animateWithDuration:duration animations:^{

        CGRect frame = textInputView.frame;
        if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
        {
            self.kHeight = kbSize.height;
        }
        else if(UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
        {
            self.kHeight = kbSize.width;
        }
        NSLog(@"keyboard up y =%f",self.kHeight);
        frame.origin.y -= self.kHeight;
        textInputView.frame = frame;

        frame = bubbleTable.frame;
        frame.size.height -= self.kHeight;
        bubbleTable.frame = frame;
    }];

它正在工作,但您注意到 UIview 不像在 facebook 或 viber 应用程序中那样顺利移动。所以我想问一下这个的常用方法是什么。十分感谢!

4

3 回答 3

2

将视图嵌入到滚动视图中(如果您使用的是 Interface Builder,则只需单击视图,然后从顶部菜单中单击 Editor->Embed In->Scroll View。以下是一些示例代码:

- (void)keyboardWasShown:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    CGSize kbSize = [info[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    // Flip kbSize if landscape.
    if (UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
        kbSize = CGSizeMake(kbSize.height, kbSize.width);
    }

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 1.0);
    [scrollView setContentInset:contentInsets];
    [scrollView setScrollIndicatorInsets:contentInsets];

    CGPoint scrollPoint = CGPointMake(0.0, [bubbleTable frame].origin.y - kbSize.height);
    [scrollView setContentOffset:scrollPoint animated:YES];
}
于 2013-10-16T15:57:54.867 回答
2

斯威夫特 3

您始终可以通过引用其约束(即使用 Outlets)来移动任何视图或 UIKit 对象

@IBOutlet weak var someViewBottomConstraint: NSLayoutConstraint!

然后,在 viewWillLayoutSubviews() 中注册一个通知

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: .UIKeyboardWillHide, object: nil)
}

完成后,您需要在 viewWillLayoutSubviews() 中调用的 2 个方法

func keyboardWillShow(_ notification: NSNotification) {
    let keyboardSize: CGSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
    let height = min(keyboardSize.height, keyboardSize.width)
    someViewBottomConstraint.constant = height // you can add a value to the height to move it up/down.
    self.view.layoutIfNeeded() // this will auto animate the view motion with the keyboard

}

func keyboardWillHide() {
    someViewBottomConstraint.constant = 0 
    self.view.layoutIfNeeded()
}
于 2017-03-28T19:45:38.090 回答
0

试试这个动画(带有选项的实验):

[UIView animateWithDuration:duration
                          delay:0
                        options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         //your animation
                     }
                     completion:nil];
于 2013-10-16T17:57:08.790 回答