旋转设备时,使自定义视图粘在键盘顶部 [英] make custom view stick to top of the keyboard while rotating device

查看:141
本文介绍了旋转设备时,使自定义视图粘在键盘顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用仅限iPad,根视图嵌入导航控制器,导航控制器有两个子视图:

My app is iPad only and the root view is embedded in a navigation controller which has two sub views:

1)a UITextView (不是 UITextField ),它涵盖整个区域,导航栏除外。

1) a UITextView (not UITextField) which covers the whole area except for the navigation bar.

2 )另一个 UIView 作为工具栏。它涵盖了UITextView,最初位于根视图的底部。

2) another UIView which serves as a tool bar. It covers the UITextView and initially stays at the bottom of root view.

现在我可以让工具栏与虚拟键盘同步上下移动。

Now I can make the "tool bar" goes up and down in sync with the virtual keyboard.

但有一个问题:如果我在键盘显示时旋转设备,工具栏不再粘在顶部虚拟键盘,而是在旋转时停留在屏幕中间,然后跌落以适应旋转后的键盘 ,这非常难看。

But there is one problem: if I rotates the device while the keyboard is showing, the "tool bar" no longer sticks to the top of virtual keyboard, instead it stays in the middle of the screen while rotating and falls down to meet the keyboard after rotation, which is quite ugly.

目前,我通过动态添加工具栏视图上下移动并删除对它的约束,我不确定这是否是一个问题,因为我只是使用模拟器测试它。

Currently I make the tool bar view goes up and down by dynamically adding and removing constraints on it and I am not sure whether this is a problem because I am only testing it using simulator.

任何人都可以给我一些建议吗?

Can anyone give me some advice on it?

底部应用程序中带有工具栏的 UITextView示例可能是
Document Pro 页面

推荐答案

我建议使用 inputAccessoryView UITextView的属性

UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
toolbar.items = [NSArray arrayWithObjects:
                       [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:nil action:nil],
                       [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil],
                       [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:nil],
                       [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:nil],
                       nil];
[toolbar sizeToFit];
self.textfiled.inputAccessoryView = toolbar;

只需使用上面的内容,您就可以在键盘顶部放置一个工具栏,它会很棒旋转期间平滑动画。我没有看过你引用的其他应用,但我相信他们在 Pages

Simply with the above you will a toolbar sticking on top of the keyboard and it will have a nice and smooth animation during rotation. I haven't looked at the other app that you quotes, but I believe that's what they use in Pages.

这篇关于旋转设备时,使自定义视图粘在键盘顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆