簡體   English   中英

阻止UIWebView重新定位輸入字段

[英]Prevent UIWebView for Repositioning for input field

我有一個iPad應用程序,我在UIPopoverController中將輸入表單顯示為UIWebView。 我的彈出控制器的大小使得在鍵盤出現時不必調整大小。

當我點擊UIWebView中的輸入字段時,當鍵盤出現時,Web視圖會被進一步向上推。 它被推到文本字段位於框架頂部的點。 (這是您在移動Safari中使用表單時看到的標准行為)。

在鍵盤啟動之前,webview根本不會滾動,但是一旦它出現,(如第二張圖片所示)我可以將webview向下滾動到正確的位置。

因為我的webview已經正確調整大小,所以我不希望出現這種情況。 有沒有人對如何防止這種情況有任何建議? (此處不使用網頁視圖目前不是一種選擇)

謝謝!

沒有鍵盤的視圖查看與鍵盤

  1. UIKeyboardWillShowNotification添加到viewDidLoad NSNotificationCenter

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
  2. 實現keyboardWillShow:和readjustWebviewScroller方法

     - (void)keyboardWillShow:(NSNotification *)aNotification { [self performSelector:@selector(readjustWebviewScroller) withObject:nil afterDelay:0]; } - (void)readjustWebviewScroller { _webView.scrollView.bounds = _webView.bounds; } 

這對我有用。

我不是Objective-C程序員(事實上我根本不了解Objective-C :-),但我也需要這樣做(在我的Web應用程序中運行在iPad上的WebView),所以谷歌的“小”幫助我這樣做了:

- (void)init {
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(keyboardWillShow:)
     name:UIKeyboardWillShowNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)aNotification {

    float x = self.webView.scrollView.bounds.origin.x;
    float y = self.webView.scrollView.bounds.origin.y;
    CGPoint originalOffset = CGPointMake(x, y);

    for (double p = 0.0; p < 0.1; p += 0.001) {
        [self setContentOffset:originalOffset withDelay:p];
    }
}

- (void)setContentOffset:(CGPoint)originalOffset withDelay:(double)delay {
    double delayInSeconds = delay;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self.webView.scrollView setContentOffset:originalOffset animated:NO];
    });
}

我知道這不是最好的解決方案 - 但它確實有效。 從一般編程的角度來看(我不知道Objective-C)我想有可能覆蓋UIScrollView類的setContentOffset方法並實現自己的行為(並且可能調用父類的超級方法)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM