Scrolling a UITextview with UIScrollview - ios

So I've created an UITextView and put it inside an UIScrollView. However, I disabled scrolling for UITextView, so I was wondering is it possible for me to scroll the textview at the same time while I'm scrolling through my UIScrollView?
I'm not sure if someone has already asked something like this, I tried to search through the site and couldn't find any topics related to my problem. If there's an existing post like this I would appreciate it if someone plink me to it.

UIScrollView is a scrolling UIView. So you need just to add the UITextView to the scroll view as a subview and it will scroll with it, basically something like this:
UIScrollView *scrollView = [UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,460)];//adjust the frame to fit your needs
scrollView.contentSize = CGSizeMake(400, 600);//make sure the size in contentSize is bigger than the size of the scrollView frame: (400, 600) is bigger than (320, 460), otherwise the scroll effect won't be performed
UITextView *textView = [UITextView alloc]initWithFrame:CGRectMake(0,0,200,400)];//adjust the frame to fit your needs
[scrollView addSubview:textView];//need to add the text view as a subview
[self.view addSubview:scrollView];

Related

Image and Html content on app screen with combined scrolling

Problem
I want to display an image and Html text on the app screen. The text is large so it may not fit on a screen so I want the content to be scrollable. WebView by default has scroll functionality for content going beyond screen size. But I don't want just the text to move upwards as I scroll down but also the image to move upwards i.e. I do not want the image to stick around on the screen when I scroll downwards.
One simple solution for this is to have the image rendered within the html content, but I do not want to do that because ImageView allows me additional functionality that I want to use.
Possible solution
The solution I tried for this was having an ImageView and a WebView within a ScrollView but when I try implementing it, only the webview content is scrolling and the image is fixed. (This is possibly because the WebView's scroll is getting preference over the parent ScrollView's scroll. I even tried setting WebView's child scroll view's delegate to outer scroll View but that didn't help and html text was not scrollable at all after build:
webview.scrollView.delegate = self.scrollView
How can I solve the problem of combined scrolling for ImageView and content in WebView possibly using other container views (if necessary)?
It sounds like you could use a UITableView with a UIImageView as your tableViewHeader. The UIWebView component could be a UITableViewCell or a tableFooterView depending on how you want to architect your view.
Set your UIImageView as your UITableView's tableHeaderView. That way, as the table scrolls, your image will scroll off screen.
Create a UITableViewCell that contains a UIWebView. You can set the height of the cell to whatever your content size is, or the screen size. The webview itself will be scrollable in this cell, and the user also is able to scroll back up to see the image header.
You can try this method :
Disable the scrolling of webview. Place the image on top of webview.
self.yourScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.yourWebView = [[UIWebView alloc] initWithFrame:CGRectMake(x position, y position, width, height)];`
self.yourWebView.opaque = NO;
self.yourWebView.scrollView.scrollEnabled = NO;
self.yourNewsArticleImageData = ({
UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x position, y position, width, height)];
imgView.contentMode = UIViewContentModeScaleAspectFit;
imgView.backgroundColor = [UIColor clearColor];
imgView;
});
[self.yourScrollView addSubview:self.yourWebView];
[self.yourScrollView addSubview:self.yourNewsArticleImageData];
self.yourScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
self.yourScrollView.scrollEnabled = YES;
[self.view addSubview:self.yourScrollView];
Main thing you need to do is disable the scrolling of webview and enable scrolling of your scroll view. Put image and webview content as subviews of scrollview.
You can change the height of the scroll view in the webview didFinish delegate method. Once you get the height of the webView loaded content change the "content size" of scroll view accordingly.

UIScrollView won't scroll to reveal more content

I've been working on this for the past couple of hours and can't figure this out.
I have a scrollview with content added to it as a subview, but my scroll view won't scroll past the tabBarController, even though the content goes beyond this point.
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height)];
self.scrollView.contentSize = CGSizeMake(320, self.scrollView.frame.size.height);
self.scrollView.autoresizesSubviews = YES;
self.scrollView.delegate = self;
[self.view addSubview:self.scrollView];
I figured this would work, but definitely doesn't. I've been searching for an answer and can't find one, help is much appreciated!
If your scrollviews frame and contentSize are the same height, it won't be able to scroll, the contentSize should be higher than your scrollview frame in order to make the scrollerview be able to scroll.
JonLOo’s answer covers the original question. To address your new question:
How do I get the scroll view to automatically adjust how much scroll is needed based on how much content there is?
Read through Apple’s Technical Note TN2154:
UIScrollView And Autolayout, particularly the Pure Auto Layout Approach section. Essentially, the constraints on the content views must fully specify a size, which the scroll view uses as its content size.
On method: viewDidLoad you will get different height of your scrollView. Try to get on viewDidAppear
Try also:
scrollView.scrollEnabled = YES;

UITextView scrolling issue when image is added as a subview

I have a fixed size UITextView and if I add more word's, it will scroll to it's content. I'm adding an UIImage as a subview in UITextView. If there are more comment's I cannot scroll UITextView to see the image. Can we set the content height of uitextview to solve this issue?
UITextView is not designed to receive a subview like that, and it won't scroll it correctly. You need to add both as subviews in a UIScrollView, which should auto-behave like so:
Swiping should first scroll the view where the touchDown event began (i.e., your UITextView)
When that view has no more to scroll, the event should bubble up to the UIScrollView itself, bringing up your UIImage.
If you don't like this behavior then you can synchronize scrolling as you prefer by calculating the height of your text inside your UITextView bounds, using:
CGSize size = [myText sizeWithFont:myFont forWidth:myFontWidth.0 lineBreakMode:UILineBreakModeWordWrap];

UIScrollView keep scroll rect while resizing

I have got a UIScrollView with a UIImageView as subview. The contentSize of the ScrollView has been set to the size of the UIImageView.
I implemented zooming and scrolling of the UIImageView.
I need the UIScrollView to keep the scroll rect when I change its frame (proportionally).
How is this done?
My problem is, whenever I change the frame of the UIScrollView the visible rect of the UIImageView changes.
Thanks!
If I understand your problem correctly, you want to shrink the frame of the scroll view without changing what you are able to see inside of the scroll view. You can do this in the following way:
Create a new UIView and set its frame to the position and size that you want to see the content of the scroll view. Turn on the clips to bounds on that view, view.clipsToBounds = yes. Add the scroll view as a subview to the view you created. [view addSubview:scrollview]. Turn clips to bounds off on the scrollview, scrollview.clipsToBounds = no.
Hope that helps.

UIScrollView - Zooming problems (Cuts off the bottom of UIView inside UIScrollView after any zooming)

So a little explanations of what my view is doing before the problem, the page loads a bunch of buttons that are clickable into the contentView which is a subView of my scrollView.
i got a UIViewController, inside i have my
UIScrollView scrollView to handle the scrolling and zooming
UIView contentView which i add all the UIButtons to.
Alright i'll try to only put the code which i believe might need to be changed.
in viewDidLoad
scrollView.contentSize = CGSizeMake(320, (20+totalRows*OFFSET_Y) );
[scrollView setMaximumZoomScale:4];
[scrollView setMinimumZoomScale:1];
[scrollView setDelegate:self];
[contentView setFrame:CGRectMake(0, 0, 320, (20+totalRows*OFFSET_Y) )];
[self.scrollView addSubview:contentView];
Doing some calculation to see how big i need to content size to be to fit all the buttons, then i add the contentView as subView to my scrollView.
I then add all the UIButtons as subview of my contentView
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [self contentView];
}
Got my delegate method for zooming returning the contentView.
Ok so everything is scrollable and zoomable like it should be..all my UIButtons are still clickable and works how it should be.
The problem is after i zoom in/out when i scroll all the way to the bottom of my scrollView it's being cut off from the bottom, so the last row of buttons are being cut in half. even when i rezoom to 1:1 it's still cut off.
I've got a NavigationController in the app which is 44 pixels, not sure if that's screwing something up somehow.
I was checking the contentSize of the scrollView and contentView and before any zooming is done, the Height of my scrollView is 44 pixels bigger than the contentView, but after any zooming the ratio is 1:1 and that seems to be the problem.
I'm not changing the size anywhere in the code though.
Any help is appreciated!
Thanks
Like i thought before I added this piece of code
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale (float)scale
{
self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, self.scrollView.contentSize.height + (44 * scale));
}
And now Zooming doesn't cut anything off and seems to be working correctly.
I still don't believe this is what I should have to do but I guess it works for now.
Still hoping someone has a better answer!
After playing more with my app I realized that the zooming was never the problem to begin with. The actual problem was with my UIView *contentView. After changing the background colour of my scrollView and my contentView (to 2 very diff colours) I noticed that my contentView wasn't long enough to cover all of the buttons.
So at first load the scrollView was big enough to see everything but once you zoom and it takes the size of the contentView it wasn't being adjusted properly!
All that to say, it finally works how it should!..haha no replies but hopefully someone might find this useful! Best tip was to change the background colours.
Good Day,
I realize very late to the game, but wanted to updated what i found on this, should anyone else come across this issue.
All did was set BOTH the UIImageView and the UIScrollView to "Aspect Fit" and this fixed the issue for me. This may be an Xcode 7 feature, but it works.

Resources