簡體   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