我一直在想,是否有可能在不使用任何私有API调用的情况下,在消息应用程序中复制苹果iOS5键盘的行为。当您在消息应用程序中向下滚动键盘时,键盘会折叠,从而留下更多空间来查看消息 - 试试看。
我找不到任何指向这样做的线索,除非开始跳跃以获取键盘视图的实例。而且我相信苹果不会对此感到满意。
除了下面给出的答案,您还可以在此处查看我的实现的完整xcode项目:https://github.com/orta/iMessage-Style-Receding-Keyboard
我一直在想,是否有可能在不使用任何私有API调用的情况下,在消息应用程序中复制苹果iOS5键盘的行为。当您在消息应用程序中向下滚动键盘时,键盘会折叠,从而留下更多空间来查看消息 - 试试看。
我找不到任何指向这样做的线索,除非开始跳跃以获取键盘视图的实例。而且我相信苹果不会对此感到满意。
除了下面给出的答案,您还可以在此处查看我的实现的完整xcode项目:https://github.com/orta/iMessage-Style-Receding-Keyboard
iOS 7中UIScrollView有一个名为keyboardDismissMode的属性。 只需将其设置为“UIScrollViewKeyboardDismissModeInteractive”,您就可以获得此行为。适用于UIScrollView的子类,例如UITableView。
self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
Swift 3:
tableView.keyboardDismissMode = .interactive
如果使用storyboard,则可以在属性检查器中更改UIScrollView子类的属性。
这是一个不完整的解决方案,但它应该为您提供一个很好的起点。
将以下ivars添加到您的UIViewController中:
CGRect keyboardSuperFrame; // frame of keyboard when initially displayed
UIView * keyboardSuperView; // reference to keyboard view
将inputAccessoryView添加到您的文本控制器中。我创建了一个小视图作为accessoryView插入:
accView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
accView.backgroundColor = [UIColor clearColor];
textField.inputAccessoryView = accView;
我将上述代码加入到-(void)loadView
中
当视图加载时,注册以接收UIKeyboardDidShowNotification和UIKeyboardDidHideNotification:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
return;
}
为指定的选择器添加作为通知的方法:
// method is called whenever the keyboard is about to be displayed
- (void)keyboardWillShow:(NSNotification *)notification
{
// makes keyboard view visible incase it was hidden
keyboardSuperView.hidden = NO;
return;
}
// method is called whenever the keyboard is displayed
- (void) keyboardDidShow:(NSNotification *)note
{
// save reference to keyboard so we can easily determine
// if it is currently displayed
keyboardSuperView = textField.inputAccessoryView.superview;
// save current frame of keyboard so we can reference the original position later
keyboardSuperFrame = textField.inputAccessoryView.superview.frame;
return;
}
添加跟踪被触摸的方法并更新键盘视图:
// stops tracking touches to divider
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGRect newFrame;
CGRect bounds = [[UIScreen mainScreen] bounds];
newFrame = keyboardSuperFrame;
newFrame.origin.y = bounds.size.height;
if ((keyboardSuperView.superview))
if (keyboardSuperFrame.origin.y != keyboardSuperView.frame.origin.y)
[UIView animateWithDuration:0.2
animations:^{keyboardSuperView.frame = newFrame;}
completion:^(BOOL finished){
keyboardSuperView.hidden = YES;
keyboardSuperView.frame = keyboardSuperFrame;
[textField resignFirstResponder]; }];
return;
}
// updates divider view position based upon movement of touches
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch;
CGPoint point;
CGFloat updateY;
if ((touch = [touches anyObject]))
{
point = [touch locationInView:self.view];
if ((keyboardSuperView.superview))
{
updateY = keyboardSuperView.frame.origin.y;
if (point.y < keyboardSuperFrame.origin.y)
return;
if ((point.y > updateY) || (point.y < updateY))
updateY = point.y;
if (keyboardSuperView.frame.origin.y != updateY)
keyboardSuperView.frame = CGRectMake(keyboardSuperFrame.origin.x,
point.y,
keyboardSuperFrame.size.width,
keyboardSuperFrame.size.height);
};
};
return;
}
免责声明:
测试这个概念我创建了一个SlidingKeyboard项目,位于BindleKit的examples目录中:
https://github.com/bindle/BindleKit
编辑:更新示例,以解决第一个免责声明。
if (point.y < keyboardSuperFrame.origin.y) return;
替换为 if (point.y < keyboardSuperFrame.origin.y) { if (keyboardSuperView.frame.origin.y != keyboardSuperFrame.origin.y) { keyboardSuperView.frame = keyboardSuperFrame; } return; }
来修复。 - Ginnypoint = [touch locationInView:self.view];
- GinnyVladimir的简单解决方案可以在用户向下滚动时隐藏键盘。然而,为了完成关于iMessage的问题,为了使TextField始终可见并锚定在键盘顶部,您需要实现以下方法:
- (UIView *) inputAccessoryView {
// Return your textfield, buttons, etc
}
- (BOOL) canBecomeFirstResponder {
return YES;
}
这是一篇很好的教程,更加详细地介绍了它。