繁体   English   中英

键盘显示时iOS横向视图移动文本字段

[英]iOS landscape view moving text fields when keyboard shows

我有一部要添加横向支持的iPhone。 这只是以前的肖像。

键盘显示时,我无法将文本字段移开。

我使用的解决方案与此处记录的Apple解决方案非常相似。

该解决方案在纵向模式下工作正常,但是当我进入横向模式时根本无法工作。 视图不是向上滚动,而是向下滚动。

我认为可能与键盘的高度/宽度有关,我认为这是正确的。

任何指出我的愚蠢或更好的解决方案的建议都值得赞赏。

- (void)keyboardWillShow:(NSNotification *)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGRect activeFieldFrame = activeField.frame;
    CGFloat kbHeight;
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
    {
        kbHeight = kbSize.height;
    }
    else
    {
        kbHeight = kbSize.width;
    }

    UIEdgeInsets contentInsets;

    contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbHeight, 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    CGRect aRect = self.view.frame;
    aRect.size.height -= kbHeight;
    activeFieldFrame.origin.y += scrollView.frame.origin.y+aRect.origin.y;
    if (!CGRectContainsRect(aRect, activeFieldFrame) )
    {
        CGPoint scrollPoint = CGPointMake(0.0, activeFieldFrame.origin.y-kbHeight-aRect.origin.y);
        [scrollView setContentOffset:scrollPoint animated:YES];
    }
}

注意,我将在“键盘显示”中执行此操作,因为我认为在显示键盘之前移动视图会更好。

好的,我将用解决方案回答我自己的问题。 当然,发布到SO之后,我似乎碰到了解决方案。

我的数学全错了。

这是我的更新版本,以防万一。

这将同时适用于风景和肖像。

- (void)keyboardWillShow:(NSNotification *)aNotification
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    {
        NSDictionary* info = [aNotification userInfo];
        CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
        CGRect activeFieldFrame = activeField.frame;
        CGRect scrollViewFrame = scrollView.frame;//used so I can see values below while debugging
        CGFloat kbHeight;
        if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
        {
            kbHeight = kbSize.height;
        }
        else
        {
            kbHeight = kbSize.width;
        }

        UIEdgeInsets contentInsets;

        contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbHeight, 0.0);
        scrollView.contentInset = contentInsets;
        scrollView.scrollIndicatorInsets = contentInsets;

        // If active text field is hidden by keyboard, scroll it so it's visible
        // Your application might not need or want this behavior.
        CGRect aRect = self.view.frame;
        aRect.size.height -= kbHeight;
        activeFieldFrame.origin.y += scrollViewFrame.origin.y+aRect.origin.y;
        if (!CGRectContainsRect(aRect, activeFieldFrame) )
        {
            //add +5 here to give just a little room between field and keyboard
            //subtracting scrollviewFrame.origin.y handles cases where scrollview is not at top (0,0) of the view
            CGPoint scrollPoint = CGPointMake(0.0, activeFieldFrame.origin.y + activeFieldFrame.size.height + 5 - aRect.size.height  - scrollViewFrame.origin.y);
            [scrollView setContentOffset:scrollPoint animated:YES];
        }

    }
}

我碰到的一件有趣的事情是,如果您注册以在视图A上获取键盘通知,并在视图A的顶部弹出模式视图B,而模式视图B也需要键盘提示,因此您需要注册模式视图B来处理它们,视图A仍然会获取键盘事件并执行代码,即使它在屏幕上不可见!

如果原始视图A是基于选项卡的设计的一部分,而另一个视图C的编辑字段由另一个选项卡显示,则原始视图A仍将使键盘显示事件。

在视图消失时应注销注册监听键盘事件,而在视图出现时应注册注册事件,这似乎是合乎逻辑的。

希望这可以帮助其他人。

当您的文本字段成为第一响应者时,滚动视图通常会为您滚动。 我曾经不希望这样做,到目前为止,我发现的唯一解决方法是将文本字段包装在与该字段大小相同的滚动视图中,因此自动滚动行为会影响它们,而不是我的主滚动条视图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM