Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (e.g 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

like image 228
wgpubs Avatar asked Nov 04 '22 17:11

wgpubs


2 Answers

I was able to get this working by setting the UITextView's text ONLY when needed (e.g. 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.

like image 134
wgpubs Avatar answered Nov 11 '22 05:11

wgpubs


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.

like image 45
Szhlopp Avatar answered Nov 11 '22 06:11

Szhlopp