Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用UIScrollview在屏幕上的键盘上方移动UIToolBar_Ios_Objective C_Uiscrollview_Autolayout - Fatal编程技术网

Ios 使用UIScrollview在屏幕上的键盘上方移动UIToolBar

Ios 使用UIScrollview在屏幕上的键盘上方移动UIToolBar,ios,objective-c,uiscrollview,autolayout,Ios,Objective C,Uiscrollview,Autolayout,我在iPhone应用程序中有一个由UITextView组成的屏幕。此文本视图包含在UIScrollView中。屏幕的用途是让用户输入文本,并可以选择将图像附加到他正在书写的内容上。因此,屏幕底部还有一个带摄像头按钮的UIToolbar。屏幕的结构如下所示: -View --UIScrollView ---UITextView --UIToolbar ---UIButton 当用户导航到此屏幕时,ViewDidDisplay方法将第一响应者分配给uitextview元素,因此键盘显示,隐藏工具栏

我在iPhone应用程序中有一个由UITextView组成的屏幕。此文本视图包含在UIScrollView中。屏幕的用途是让用户输入文本,并可以选择将图像附加到他正在书写的内容上。因此,屏幕底部还有一个带摄像头按钮的UIToolbar。屏幕的结构如下所示:

-View
--UIScrollView
---UITextView
--UIToolbar
---UIButton
当用户导航到此屏幕时,ViewDidDisplay方法将第一响应者分配给uitextview元素,因此键盘显示,隐藏工具栏和相机按钮

我希望整个工具栏在键盘上方重新绘制,并在键盘隐藏时再次将其定位在屏幕底部

我在SO上找到了相关的帖子(比如)。然而,这种方法会引入不希望出现的行为。例如,实现上述文章中的解决方案时,工具栏确实随键盘移动,但UIScrollView的frame.origin.y坐标在屏幕顶部上方移动,因此用户无法看到他正在键入的内容

我还尝试重置工具栏的框架,将其添加为IBOutlet并使用cgrectmake重新定位。但是,几次尝试后,工具栏仍停留在屏幕底部,并被键盘隐藏:

- (void) liftMainViewWhenKeybordAppears:(NSNotification*)aNotification{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationCurve:<#(UIViewAnimationCurve)#>]

    CGRect frame = self.keyboardToolbar.frame;
    frame.origin.y = self.keyboardToolbar.frame.origin.y - 280;
    self.keyboardToolbar.frame = frame;

    [UIView commitAnimations];
}
-(无效)liftmainviewhenkeybordapears:(NSNotification*)通知{
[UIView beginAnimations:nil上下文:NULL];
[UIView setAnimationDelegate:self];
[UIView设置动画持续时间:0.3];
[UIView设置动画曲线:]
CGRect frame=self.keyboardToolbar.frame;
frame.origin.y=self.keyboardToolbar.frame.origin.y-280;
self.keyboardToolbar.frame=frame;
[UIView委员会];
}
我已经尝试了几次类似于上面代码的迭代,但都无法重新定位工具栏


简言之,在uitextview元素完全占用了屏幕空间的情况下,在键盘上方浮动工具栏的正确方式是什么?

多亏了RoryMcKinnel的指针。由于引用的文章是在Swift中,我想我可能会将适用于的解决方案粘贴到ObjC上

- (void)keyboardWillShowNotification:(NSNotification *)notification{
NSDictionary *userInfo = notification.userInfo;

double animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect keyboardEndFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect convertedKeyboardFrame = [self.view convertRect:keyboardEndFrame fromView:self.view.window];
UIViewAnimationOptions rawAnimationCurve = (UIViewAnimationOptions)[userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue] << 16;
_toolBarBottomGuide.constant = CGRectGetMaxY(self.view.bounds) - CGRectGetMinY(convertedKeyboardFrame);
[UIView animateWithDuration:animationDuration delay:0.0 options:rawAnimationCurve animations:^{
    [self.view layoutIfNeeded];
} completion:nil];
}
-(无效)键盘将显示通知:(NSNotification*)通知{
NSDictionary*userInfo=notification.userInfo;
double animationDuration=[userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
CGRect keyboardEndFrame=[userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
CGRect convertedKeyboardFrame=[self.view convertRect:keyboardEndFrame fromView:self.view.window];

UIViewAnimationOptions rawAnimationCurve=(UIViewAnimationOptions)[userInfo[UIKeyboardAnimationCurveUserInfo]UnsignedIntegValue]在iOS8中调整帧并不理想,因为它与约束冲突。您是否尝试过将工具栏底部的约束添加到superview中?您可以按住CTRL键将其添加到代码中,然后在调出编辑器时更改代码中的约束。因此,添加一个高度约束,即0,并在需要时将其拖动到代码中的IBOutlet中打开编辑器,将IBOutlet的
.constant
字段更改为键盘的高度。当键盘放下时,将其设置为0。您可能需要强制布局。相同的想法,但使用间隔视图:@RoryMcKinnel工作得很好,感谢指针。我很难将此代码从Swift转换为Obj-C,但它完成了tr哎呀。
- (void)keyboardWillHideNotification:(NSNotification *)notification{
    NSDictionary *userInfo = notification.userInfo;

    double animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect keyboardEndFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    UIViewAnimationOptions rawAnimationCurve = (UIViewAnimationOptions)[userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue] << 16;
    _toolBarBottomGuide.constant = 0.0f;
    [UIView animateWithDuration:animationDuration delay:0.0 options:rawAnimationCurve animations:^{
        [self.view layoutIfNeeded];
    } completion:nil];
}