我正在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应用程序那样平稳移动。所以我想问一下这是什么常见方法。非常感谢你!
答案 0 :(得分: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];
}
答案 1 :(得分:2)
Swift 3
您可以随时移动任何视图或UIKit对象,通过引用其约束(即使用Outlet)
@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()
}
答案 2 :(得分:0)
尝试此动画(使用选项进行实验):
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionBeginFromCurrentState
animations:^{
//your animation
}
completion:nil];