简体   繁体   中英

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

I have a UITextView inside of a UIScrollView that is configured offscreen when the code below runs in my ViewController's viewDidLoad . Unfortunately, if the "eventDetails" text is particularly long nothing is displayed inside the UITextView until I interact with it (eg click inside and drag for example).

My question: How to have it so the text is displayed in the UITextView WITHOUT forcing the user to interact with the UITextView first?

Here is the code:

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)];

Thanks

I was able to get this working by setting the UITextView's text ONLY when needed (eg is about to come on screen). Below is the relevant code:

- (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];
    }
}

If anyone has a better solution or needs more explanation just hit me up in the comments.

I've had the same issue and it's taken me several hours to find a suitable fix for it... Here's what I came up with...

-(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];
    }
}

I'm using CoreAnimation to switch to this view. So right before I execute that code, I call this

//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

This solves the issue. After setting the layer.bounds it redraws the control( UITextView ) and it thinks it's onscreen. Then CoreAnimation takes care of animating from my current UIView to the new UIView and all of the text in the uiTextView is displayed throughout the animation.

Looks like the views that you are setting up are not refreshed.
And when you interact with the views then they are refreshed and your changes are rendered to the display...

Try adding [txtDetails setNeedsDisplay]; [page3ScrollView setNeedsDisplay]; [txtDetails setNeedsDisplay]; [page3ScrollView setNeedsDisplay]; after your code in viewDidLoad .

If it will help then maybe we'll find some more elegant solution...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM