tags:

views:

4272

answers:

4

Hi all,

I want to have a UIScrollView with a set of subviews where each of these subviews has a UITextView with a different text. For this task, I have modified the PageControl example from the apple "iphone dev center" in order to add it a simple UITextView to the view which is used to generate the subviews of the scroll view. When I run the app (both on the simulator and the phone), NO Text is seen but if i activate the "user interaction" and click on it, the text magically appears (as well as the keyboard).

Does anyone has a solution or made any progress with UITextView inside a UIScrollView? Thanks.

+2  A: 

I think the problem stems from the fact that UITextView is a subclass of UIScrollView, so you basically have scroll views embedded within scroll views. Even if the text displayed properly, you would have usability problems, as it would never be clear if a finger swipe was supposed to scroll the outer view or the text view.

Yeah, Safari sort of does this, but it has to, and it's not the most pleasant part of using Safari.

I think this is one of those times where the difficulty indicates you are working against the system. I strongly recommend going back and re-thinking the UI.

benzado
Thanks for your answer, I also think that the bug is related to the fact that UITextView is a subclass of UIScrollView since this also happens with a UIWebView. However, I don't think that trying this, is working against the system as it is a natural way of displaying a set of steps or instructions
muesan
I agree but I don't think it's a bug, it's a feature. I don't see any way for Apple to avoid this.
Roger Nolan
A: 

Hi, it works for me by placing the text value assignment into the scrollViewDidScroll method.

Sample snippets:


SAMPLE.h

...
@interface myRootUIViewController : UIViewController <UIScrollViewDelegate>
...


Comment: Just to remember: don't forget the UIScrollViewDelegate protocol.


SAMPLE.m

    - (void)viewDidLoad {
       ... whatever is created before and/or after...

       NSString * text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
                           Nunc semper lacus quis erat. Cras sapien magna, porta non, 
                           suscipit nec, egestas in, arcu. Maecenas sit amet est. 
                           Quisque felis risus, tempor eu, dictum ac, volutpat id, 
                           libero. Ut gravida, purus vitae interdum elementum, tortor 
                           justo porttitor nisi, id rhoncus massa.";

       // calculate the required frame height according to defined font size and
       // given text
       CGRect frame = CGRectMake(0.0, 500.0, self.view.bounds.size.width, 1000.0); 
       CGSize calcSize = [text sizeWithFont:[UIFont systemFontOfSize:13.0]
              constrainedToSize:frame.size lineBreakMode: UILineBreakModeWordWrap];
              // for whatever reasons, contraintedToSize seem only be able to
              // calculate an appropriate height if the input frame height is larger
              // than required. Means: if your text requires height=250 and input
              // frame height=100, then this method won't give you the expected
              // result.

       frame.size = calcSize;
       frame.size.height += 0; // calcSize might be not pixel-precise, 
                               // so add here additional padding pixels
       UITextView * tmpTextView = [[UITextView alloc]initWithFrame:frame];

       // do whatever adjustments
       tmpTextView.backgroundColor = [UIColor blueColor]; // show area explicitly (dev 
                                                          // purpose)
       self.myTextView = tmpTextView;
       self.myTextView.editable = NO;
       self.myTextView.scrollEnabled = NO;
       self.myTextView.multipleTouchEnabled = NO;
       self.myTextView.userInteractionEnabled = NO; // pass on events to parentview
       self.myTextView.font = [UIFont systemFontOfSize:13.0];
       [tmpTextView release];
       [self.scrollView addSubview:self.myTextView];
}

...

- (void)scrollViewDidScroll:(UIScrollView *)sender {
  // for simplicity text is repeated again, of course it can be a member var/etc...
  NSString * text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
                           Nunc semper lacus quis erat. Cras sapien magna, porta non, 
                           suscipit nec, egestas in, arcu. Maecenas sit amet est. 
                           Quisque felis risus, tempor eu, dictum ac, volutpat id, 
                           libero. Ut gravida, purus vitae interdum elementum, tortor 
                           justo porttitor nisi, id rhoncus massa.";
  self.myTextView.text = text; // assign value within this method and it is
                               // painted as expected.
    }


Comment:

I have adjusted the source code snippet with sample namings and values obviously. Hopefully there is no typo. However, the code contains also the calculation of the required frame height for the text, in case the text's value can change and therefore would require different frame sizes actually.

Placing the actual text value assignment into the scrollViewDidScroll method worked for me without any kind of flashing during scrolling etc. (so far only tested in iPhone Simulator).

Hope that helps. Of course I am open for any constructive feedback, improvement proposals or even other ways to solve this isuse.

filmore
assigning the text ViewDidScroll sounds like the wrong thing to do.
Roger Nolan
A: 

The problem is likely to be the nexted UIScrollViews.

I think there are three solutions:

  • Enable and disable userInteractionEnabled as the various controls are selected.
  • Assuming the text is static, use a UILabel instead of a UITextView (UILabel is not a subclass of UIScrollView)
  • Implement your own view and draw the text yourself in a drawRect message rather than relying on the UITextView.
Roger Nolan
A: 

You may be suffering from the problem where UITextView's don't update properly when they are scrolled from an offscreen to an onscreen area.

Check the "Offscreen UITextViews don't update correctly" section of this page: Multiple Virtual Pages in a UIScrollView

The solution I used was to force a redraw of scroll views when they begin to appear onscreen. This is a complete nuisance but does fix the problem.

Matt Gallagher