旋转设备时如何让 inputAccessoryView 调整大小? [英] How should I get my inputAccessoryView to resize when rotating device?

查看:24
本文介绍了旋转设备时如何让 inputAccessoryView 调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 UIToolbar 附加到我的 UITextView 作为它的 inputAccessoryView 以添加一个按钮来关闭键盘.这很好用,并且当设备处于纵向模式时看起来是正确的.但是,当设备处于横向模式时,我无法弄清楚如何将工具栏的大小调整为用于工具栏的较低高度.

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.

我在文本视图的委托的 -textViewShouldBeginEditing: 方法中添加工具栏:

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.

推荐答案

这是一个棘手的问题.我过去通过在旋转后布置附件视图时调整框架来解决这个问题.尝试这样的事情:

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:

@interface RotatingTextInputToolbar : UIToolbar
@end

@implementation RotatingTextInputToolbar

- (void) layoutSubviews
{
    [super layoutSubviews];
    CGRect origFrame = self.frame;
    [self sizeToFit];
    CGRect newFrame = self.frame;
    newFrame.origin.y += origFrame.size.height - newFrame.size.height;
    self.frame = newFrame;

}
@end

并在上面的代码中使用 RotatingTextInputToolbar 而不是 UIToolbar.

And using the the RotatingTextInputToolbar instead of the UIToolbar in your code, above.

这篇关于旋转设备时如何让 inputAccessoryView 调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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