Alb*_*ert 13 objective-c ios ios8.3
我可以处理两个事件:键盘显示和键盘隐藏时.在iOS 8.2及更早版本中,一切都运行良好.
但是如何在更改键盘语言时处理事件?当您将英文键盘更改为表情符号键盘时,表情符号键盘的高度(在ios 8.3中)更大并且隐藏了内容.
或者你有一个解决方案如何控制iOS 8.3表情符号键盘高度?
n00*_*mer 13
好.所以看着我的旧代码,我记得,我不使用2个观察者(UIKeyboardDidShowNotification
/ UIKeyboardDidHideNotification
).我使用单个观察者(UIKeyboardWillChangeFrameNotification
),即每个事件被触发:键盘隐藏,键盘显示,键盘更换框架.
在我的例子中,文本框和发送按钮嵌套在一个UIView
,这是视图添加在其中view
的UIViewController
所有其他内容.
我添加观察者viewDidAppear
并删除观察者viewWillDisappear
.(以避免在视图未激活时触发任何通知).
以上信息对您的情况不是必需的,只是为了提供信息而添加它.相关代码如下:
添加观察员:
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
Run Code Online (Sandbox Code Playgroud)
手柄通知:
- (void) keyboardWillChangeFrame:(NSNotification*)notification {
NSDictionary* notificationInfo = [notification userInfo];
CGRect keyboardFrame = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
[UIView animateWithDuration:[[notificationInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]
delay:0
options:[[notificationInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue]
animations:^{
CGRect frame = self.textViewContainer.frame;
frame.origin.y = keyboardFrame.origin.y - frame.size.height;
self.textViewContainer.frame = frame;
} completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
您可能需要对frame.origin.y...
线进行一些调整才能进行正确的计算.我不知道你UITabBarController
底部是否有任何一根柱子.这里最安全的赌注是:
frame.origin.y = self.view.frame.size.height - keyboardFrame.size.height - X;
Run Code Online (Sandbox Code Playgroud)
X
如果您的VC覆盖整个屏幕,那么其中0.如果没有,请使用任何底栏的高度.
值得注意的是,表情符号键盘与启用建议文本的标准键盘高度相同。
要正确确定键盘高度并调整视图,请添加以下观察者:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
Run Code Online (Sandbox Code Playgroud)
然后我使用以下方法来动画键盘调整。实际上,您所需要的只是keyboardBounds
对象,但如果您碰巧使用 AutoLayout,则可以这样做:
- (void)keyboardDidShow:(NSNotification *)notification
{
[self scrollControlBarTo:notification up:YES];
}
-(void)keyboardDidHide:(NSNotification *)notification
{
[self scrollControlBarTo:notification up:NO];
}
- (void)scrollControlBarTo:(NSNotification *)notification up:(BOOL)up
{
[_keyboardControlsBar layoutIfNeeded];
CGRect keyboardBounds;
NSDictionary *info = [notification userInfo];
NSNumber *number = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
double duration = [number doubleValue];
[[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardBounds];
UIViewAnimationCurve curve = [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[UIView setAnimationCurve:curve];
_keyboardControlsBarBottomConstraint.constant = (up) ? keyboardBounds.size.height : 0;
[self.view layoutIfNeeded];
} completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2977 次 |
最近记录: |