簡體   English   中英

使用UIScrollView滑動並在使用鍵盤時向上移動視圖

[英]swiping with UIScrollView and moving up the view when using keyboard

我正在使用這個示例項目,我想要做的是當你在視圖上放置一個UITextField時,當鍵盤下方的UITextField ,視圖向上移動了一點。

我已經使用了這個項目中的TPKeyBoardAvoidingScrollView類。 然而,當我單擊UITextField ,視圖向上移動並且一切都很好但是當我單擊背景時,它不是僅恢復正常的屏幕大小並解除鍵盤,而是返回到第一個視圖而不是粘在視圖中我們就在那一刻。

當彈出鍵盤時,你可以左右滾動,任何想法我怎么解決這個問題? 是我的項目,我將它們加在一起。

在TPKeyboardAvoidingScrollView.m中有一個方法(keyboardWillHide :),當鍵盤要隱藏時會調用它。 在該方法中,scrollview的內容偏移設置為CGPointZero,因此您的scrollview將轉到第一個視圖控制器。

- (void)keyboardWillHide:(NSNotification*)notification {
    _keyboardRect = CGRectZero;
    _keyboardVisible = NO;

    // Restore dimensions to prior size
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
    [UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
    self.contentInset = _priorInset;
    //self.contentOffset = CGPointZero;//Replacing this with below line
    self.contentOffset = CGPointMake(self.contentOffset.x, 0);
    [self setScrollIndicatorInsets:self.contentInset];
    _priorInsetSaved = NO;
    [UIView commitAnimations];
}

要在編輯文本字段時停止滾動 -

- (void)keyboardDidShow:(NSNotification*)notification {
    [self setScrollEnabled:NO];
    //existing code
}

- (void)keyboardWillHide:(NSNotification*)notification {
    [self setScrollEnabled:YES];
    //existing code with modification of content offset
}

請記住,這可能會影響您使用TPKeyboardAvoidingScrollView對象的其他視圖。

暫無
暫無

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

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