簡體   English   中英

自動滾動UIScrollView以適合本地iOS Mail.app中的內容

[英]Autoscroll UIScrollView to fit content like in native iOS Mail.app

創建新字母時,本機iOS Mail.app具有強大的功能。 整個屏幕是一個UIScrollView ,而寫字母正文的位置是一個UITextView ,該滾動被禁用。

在此處輸入圖片說明

當您鍵入此TextView的高度以及UIScrollView的高度時,其動態變化,並且UIScrollView向下滾動,從而在鍵盤上方留出一些像素來輸入新文本。

我知道這個過程已在做textViewDidChange的方法,但在試圖做同樣的,不順心的事在我的代碼- UITextField有時可能會往下走,下UIScrollView 這是我嘗試執行的操作:

-(void)textViewDidChange:(UITextView *)textView {

    CGRect frame = emailTextView.frame;
    frame.size.height = emailTextView.contentSize.height;
    emailTextView.frame = frame;
    mainScrollView.contentSize = CGSizeMake(320, emailTextView.contentSize.height + rightKeyboardSize.height + 20);

}

對這里出什么問題有任何想法嗎? 提前致謝!

好的,我實施了自己的示例項目來找到答案。 您應該替換您的名字。

首先,我將添加觀察者以檢測何時顯示或隱藏鍵盤:

- (void)addKeyboardObserver
{
    // This could be in an init method.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}

- (void)keyboardDidShow:(NSNotification*)notification
{
    NSDictionary* keyboardInfo = [notification userInfo];
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
    _keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
    UIScrollView *_scrollView = (UIScrollView*)self.view;
    _scrollView.frame = CGRectMake(_scrollView.frame.origin.x,
                                   _scrollView.frame.origin.y,
                                   _scrollView.frame.size.width,
                                   _scrollView.frame.size.height - _keyboardFrameBeginRect.size.height);
}

- (void)keyboardDidHide:(NSNotification*)notification
{
    UIScrollView *_scrollView = (UIScrollView*)self.view;
    _scrollView.frame = CGRectMake(_scrollView.frame.origin.x,
                                   _scrollView.frame.origin.y,
                                   _scrollView.frame.size.width,
                                   _scrollView.frame.size.height +
                               _keyboardFrameBeginRect.size.height);
}

然后,將textViewDidChange:方法更改為:

- (void)textViewDidChange:(UITextView *)textView
{
    UIScrollView *_scrollView = (UIScrollView*)self.view;
    _textView.frame = CGRectMake(_textView.frame.origin.x,
                                 _textView.frame.origin.y,
                                 _textView.contentSize.width,
                                 _textView.contentSize.height);
    _scrollView.contentSize = _textView.frame.size;

    if (_scrollView.frame.size.height < _textView.frame.size.height) {
        CGPoint bottomOffset = CGPointMake(0,_textView.frame.size.height-_keyboardFrameBeginRect.size.height);
        [_scrollView setContentOffset:bottomOffset animated:NO];
    }
}

祝好運!

暫無
暫無

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

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