簡體   English   中英

當父視圖是UIScrollView UNTIL I scoll時,UITextView文本不顯示

[英]UITextView text not displayed when parent view is UIScrollView UNTIL I scoll

我在UIScrollView中有一個UITextView ,當我在ViewController's viewDidLoad中運行下面的代碼時,它在屏幕外配置。 不幸的是,如果“eventDetails”文本特別長,則在我與UITextView交互之前不會顯示任何內容(例如,單擊內部並拖動)。

我的問題:如何擁有它以便文本顯示在UITextView中而不強迫用戶首先與UITextView進行交互?

這是代碼:

UITextView *txtDetails = [[UITextView alloc] initWithFrame:CGRectMake(-4, yOffset, page3ScrollView.frame.size.width, 0)];
[txtDetails setEditable:NO];
[txtDetails setScrollEnabled:NO];
[txtDetails setFont:[UIFont systemFontOfSize:12]];
[txtDetails resizeAndSetTextWithMaxSize:CGSizeMake(txtDetails.frame.size.width, 999999999) forText:eventDetails withAdditionalHeightOf:16.f];

[page3ScrollView addSubview:txtDetails];

CGRect frame = txtDetails.frame;
frame.size.height = [txtDetails contentSize].height;
txtDetails.frame = frame;

[page3ScrollView setContentSize:CGSizeMake(page3ScrollView.frame.size.width, txtDetails.frame.origin.y + txtDetails.frame.size.height)];

謝謝

通過在需要時設置UITextView的文本(例如即將出現在屏幕上),我能夠實現這一點。 以下是相關代碼:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)sv 
{
    if (sv == scrollView) [self updatePagedView];
}

- (void)updatePagedView
{
    int currentPage = pageControl.currentPage;

    // *** loadDetailsPage3 is where I set the text ***
    if (currentPage == 2) {
        [self loadDetailsPage3];
    }
}

如果有人有更好的解決方案或需要更多解釋,請在評論中點擊我。

我有同樣的問題,我花了幾個小時找到合適的解決方案...這就是我想出的......

-(void)DidBecomeActive {
    if (!DidUpdateBounds) {
        DidUpdateBounds = true; // instance variable set to false on load
        NSString *oldtext = uiTextView.text;

        //The trick is (1) removing it from it's superview
        // (2) resetting it's 'text' property
        // (3) re-adding it to the view WHILE it's on-screen.
        [uiTextView removeFromSuperview];
        uiTextView.text = oldtext;
        [self.view addSubview:uiTextView];
        [self.view setNeedsDisplay];
    }
}

我正在使用CoreAnimation切換到此視圖。 所以在我執行該代碼之前,我稱之為

//The 0,-300,480,300 is bounds of the UIView my offscreen UITextview is on
[self.view.layer setBounds:CGRectMake(0, -300, 480, 300)];

// SetNeedsDisplay, then call my method above to
//FORCIBILY redrew the UITextView while it's effectively on-screen
[self.view setNeedsDisplay];
[TextLogVC DidBecomeActive];
//CoreAnimation then goes here

這解決了這個問題。 在設置了layer.bounds之后,它重新繪制了控件( UITextView )並且它認為它在屏幕上。 然后CoreAnimation處理從我當前的UIView到新UIView的動畫,並且在整個動畫中顯示uiTextView中的所有文本。

看起來您正在設置的視圖不會刷新。
當您與視圖交互時,它們將被刷新,您的更改將呈現給顯示...

嘗試添加[txtDetails setNeedsDisplay]; [page3ScrollView setNeedsDisplay]; [txtDetails setNeedsDisplay]; [page3ScrollView setNeedsDisplay]; viewDidLoad的代碼之后。

如果有幫助那么我們可能會找到一些更優雅的解決方案......

暫無
暫無

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

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