Fixing UITextView inside UIScrollView
I have a UISCrollView named scroll1. Inside scroll1, I create programmatically a UIImageView loaded with images wrapped in new UIScrollViews. So my Architecture is scroll1-> subView-> ImageView. The object of my application is that the user scrolls the parent of the scroll (scroll1) and scrolls through the uiimages changes.
Along with the UIImageViews, a UITextView appears, containing text data. When the user scrolls, the text value changes as the user scrolls.
When the user scrolls, I want the UITextView to stay on top of the UIImageVIew as if this text view is embedded in the ImageView. Similar to scrolling through the pages of a storybook, when you scroll through text, it stays stationary with the image until the image disappears. I am using the following code:
CGSize imageSize;
imageSize.height = 693;
imageSize.width = 512;
self.scroll1.minimumZoomScale = 1.0;
self.scroll1.maximumZoomScale = 1.0;
self.scroll1.frame = CGRectMake(0,0,imageSize.width,imageSize.height);
self.scroll1.scrollEnabled = YES;
self.scroll1.contentSize = CGSizeMake(imageSize.width * imgs.count, imageSize.height );
self.scroll1.pagingEnabled = YES;
self.scroll1.delegate = self;
for (int i = 0; i < pants; i++)
{
CGFloat x = i * 512;
subView = [[UIScrollView alloc]initWithFrame:CGRectMake(x, 0, 512, 693)];
subView2 = [[UIScrollView alloc]initWithFrame:CGRectMake(x + 512, 0, 512, 693)];
subView.clipsToBounds = NO;
subView2.clipsToBounds = NO;
UIImage *img = [imgs objectAtIndex:i];
UIImage *img2;
if(i == pants - 1) img2 = [imgs objectAtIndex:i];
else img2 = [imgs objectAtIndex:i+1];
imageView = [[UIImageView alloc ] initWithImage:img];
imageView2 = [[UIImageView alloc ] initWithImage:img2];
// [imageView insertSubview:_leftText atIndex:-2];
// [imageView insertSubview:_leftText atIndex:1];
//[self.scroll1 insertSubview:_leftText aboveSubview:imageView];
//imageView.frame = subView.bounds;
// [subView insertSubview:_leftText aboveSubview:imageView];
subView.scrollEnabled = NO;
subView2.scrollEnabled = NO;
subView.userInteractionEnabled = NO;
subView2.userInteractionEnabled = NO;
[subView addSubview:imageView];
[subView2 addSubview:imageView2];
[subView setContentSize:CGSizeMake(512, 708)];
[subView2 setContentSize:CGSizeMake(512, 708)];
[self.scroll1 addSubview:subView];
[self.scroll1 addSubview:subView2];
//[self.scroll1 addSubview:_leftText];
}
The above code is used to create small subzones, leftText is textView.
As it shows, I tried various methods to add _leftText to the subView because I thought adding it would automatically make it a part of it and it would be injected, but either it doesn't show up or it doesn't work. I even tried the following method:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[_leftText setContentOffset:self.scroll1.contentOffset animated:YES];
}
But this code makes the textView behave strangely and disappears. Hope I can do it. Many thanks
source to share
Try to insert multiple instances of UITextField above each of the subspecies.
NSMutableArray *textFields=[NSMutableArray array];
for (int i=0; i<pants; i++)
{
CGFloat x=i * 512;
UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:frame];
UIImageView *imageView =[[UIImageView alloc] initWithFrame:imageFrame];
UITextField *textField =[[UITextField alloc] initWithFrame:textFrame];
[scrollView addSubview:imageView];
[scrollView addSubview:textField]; // put it above the imageView
[textFields addObject:textField];
}
So instead of text boxes instead of one. Then you need to figure out which textbox is currently being displayed in the delegate method scrollViewDidScroll:
. It's better to use the contentOffset
scroll1 property for this.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect visibleRect;
visibleRect.origin = scrollView.contentOffset;
visibleRect.size = scrollView.bounds.size;
// iterate through the textFields array to find out which textField is currently visible.
}
Good luck.
source to share