我应该如何让我的 inputAccessoryView 在旋转设备时调整大小?

发布于 2024-12-15 21:14:55 字数 1446 浏览 2 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由如风 2024-12-22 21:14:55

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

@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

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

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

梦巷 2024-12-22 21:14:55

瞧:

@interface AccessoryToolbar : UIToolbar @end

@implementation AccessoryToolbar

-(id)init
{
    if (self = [super init])
    {
        [self updateSize];
        NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
        [nc addObserver:self selector:@selector(orientationDidChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:NULL];
    }
    return self;
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

-(void)orientationDidChange:(NSNotification*)notification
{
    [self updateSize];
}

-(void)updateSize
{
    bool landscape = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
    CGSize size = UIScreen.mainScreen.bounds.size;
    if (landscape != size.width > size.height)
        std::swap(size.width, size.height);
    if (size.height <= 320)
        size.height = 32;
    else
        size.height = 44;
    self.frame = CGRectMake(0, 0, size.width, size.height);
}

@end

Voila:

@interface AccessoryToolbar : UIToolbar @end

@implementation AccessoryToolbar

-(id)init
{
    if (self = [super init])
    {
        [self updateSize];
        NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
        [nc addObserver:self selector:@selector(orientationDidChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:NULL];
    }
    return self;
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

-(void)orientationDidChange:(NSNotification*)notification
{
    [self updateSize];
}

-(void)updateSize
{
    bool landscape = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
    CGSize size = UIScreen.mainScreen.bounds.size;
    if (landscape != size.width > size.height)
        std::swap(size.width, size.height);
    if (size.height <= 320)
        size.height = 32;
    else
        size.height = 44;
    self.frame = CGRectMake(0, 0, size.width, size.height);
}

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文