我应该如何让我的 inputAccessoryView 在旋转设备时调整大小?
我将 UIToolbar
附加到我的 UITextView
作为其 inputAccessoryView
,以便添加一个按钮来关闭键盘。这非常有效,并且当设备处于纵向模式时它看起来很正确。但当设备处于横向模式时,我无法弄清楚如何将工具栏大小调整为工具栏使用的较低高度。
我在文本视图委托的 -textViewShouldBeginEditing:
方法中添加工具栏:
if (!textView.inputAccessoryView) {
UIToolbar *keyboardBar = [[UIToolbar alloc] init];
keyboardBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
keyboardBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissKeyboard:)];
[keyboardBar setItems:[NSArray arrayWithObjects:spaceItem, doneButton, nil]];
[spaceItem release];
[doneButton release];
[keyboardBar sizeToFit];
textView.inputAccessoryView = keyboardBar;
[keyboardBar release];
}
不过,在横向模式下,我从这段代码中得到了奇怪的行为。如果我在横向模式下开始编辑,工具栏具有横向高度,但“完成”按钮会在屏幕上绘制一半。如果我随后旋转到纵向模式,“完成”按钮将绘制在正确的位置,并且当我旋转回横向模式时,它仍保留在正确的位置。
如果我开始在纵向模式下进行编辑,工具栏将以纵向高度绘制,但“完成”按钮会绘制在正确的位置。如果我然后旋转到横向模式,工具栏仍保持纵向高度,但“完成”按钮至少仍绘制在正确的位置。
关于如何在设备旋转时调整其大小有什么建议吗?我真的希望有一种比在视图控制器的旋转事件之一中手动插入高度幻数更自动的方法。
I'm attaching a UIToolbar
to my UITextView
as its inputAccessoryView
in order to add a button to dismiss the keyboard. This works great and it looks correct when the device is in portrait mode. But I'm having trouble figuring out how to resize the toolbar to the lower height used for toolbars when the device is in landscape mode.
I'm adding the toolbar in my text view's delegate's -textViewShouldBeginEditing:
method:
if (!textView.inputAccessoryView) {
UIToolbar *keyboardBar = [[UIToolbar alloc] init];
keyboardBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
keyboardBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissKeyboard:)];
[keyboardBar setItems:[NSArray arrayWithObjects:spaceItem, doneButton, nil]];
[spaceItem release];
[doneButton release];
[keyboardBar sizeToFit];
textView.inputAccessoryView = keyboardBar;
[keyboardBar release];
}
I get strange behavior from this code in landscape mode, though. If I start editing in landscape mode, the toolbar has the landscape height but the Done button is drawn half off the screen. If I then rotate to Portrait mode, the Done button is drawn in the correct location, and it remains in the correct location when I rotate back to landscape mode.
If I start editing in portrait mode, the toolbar is drawn with portrait height, but the Done button is drawn in the correct location. If I then rotate to landscape mode, the toolbar remains portrait height, but the Done button is still drawn in the correct position at least.
Any suggestions for how to get this to resize when the device rotates? I'm really hoping there's a more automatic way than manually plugging in the height magic numbers in one of the view controller's rotation events.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个棘手的问题。我过去已经通过在旋转后布置附件视图时调整框架来解决这个问题。尝试这样的操作:
并在上面的代码中使用
RotatingTextInputToolbar
而不是UIToolbar
。That's a tough problem. I've solved this in the past by adjusting the frame when the accessory view gets laid out after rotating. Try something like this:
And using the the
RotatingTextInputToolbar
instead of theUIToolbar
in your code, above.瞧:
Voila: