UIScrollView ContentSize Limited to the Size of the UIScollView - ios

I have set the contentSize of the UIScrollView as follows:
self.scrollView.contentSize = CGSizeMake(320,800);
But as you can see in the image below it still displays one last line under the UITabbar. Why?
I am using iOS 7.

The scrollView's contentSize refers to the size of the content that can be scrolled through. For example you could have a contentSize height of 2000 px, because you have 2000 px worth of content to scroll through.
Your contentSize is not the problem; you indeed want it to be 800 (I'm assuming that's the height of all your text and graphics, not including the tab bar at the bottom.) What you want to change is the actual size of the scrollView object itself. Then the scrollView will be the correct height (something like scrnHeight - tabBarHeight) but the contentSize will still reflect the size of the content (the stuff inside the scrollView).
[self.scrollView setFrame:CGRectMake(self.scrollView.frame.origin.x,
self.scrollView.frame.origin.Y, 320, scrnHeight - tabBarHeight)];
self.scrollView.contentSize = CGSizeMake(320, 800);
And then you should be all set. Now it's possible that 800 isn't actually the number you want; the number you want is the exact height from the top of the scrollview to the very botom of that text at the bottom. But this is the idea :)

Related

UIScrollView doesn't work

These are my UIViews:
in my views I have 2 uitableview, 2 button.
I always check my UIView's sizes and these are the results:
randomPOC[2231:67151] MainView-1060.000000
2018-05-17 10:33:28.812877+0800 randomPOC[2231:67151] ScrollView-1060.000000
2018-05-17 10:33:28.813094+0800 randomPOC[2231:67151] ContentView-1060.000000
When my mainview , scroll and contentview have the same sizes, it doesn't scroll.
But when I divide the height into to 2 (height/2) of the mainview, it allows me to scroll. which makes me confused how and why.
when I divided the mainview height into to 2 it allows me to scroll but it doesn't covers down to the last of the tableview.
I need to resize the mainview because I need to cover the two(2) uitableview that has sometimes more than 1000height.
_mainView.frame=CGRectMake(_mainView.frame.origin.x, _mainView.frame.origin.y, _mainView.frame.size.width, uitableview2.height );
This answer has nothing to do with why your UIScrollView doesnt work.
UIScrollView will only scroll if its content size is greater than its frame size.
You should first decide the size of your UIScrollView frame, Than for a Veritical UIScrollView you should assign the content size as follow:
Lets say you have 5 UIView's which have height of 5 dp each, than you should set the UIScrollView height like so:
scrollview.setContentSize(width, 5*5).
Than if the UIScrollView frame height is greater than 25, the UIScrollView will not scroll.
Else the UIScrollView will scroll.
hope it helps!
Found the answer by using this resizing the mainview like this instead:
_mainView.frame=CGRectMake(_mainView.frame.origin.x, _mainView.frame.origin.y, _mainView.frame.size.width, [[UIScreen mainScreen]bounds].size.height );

Auto resize UITextView in UIScrollView

I have a question, how can I resize a textview in a scrollview and scrollview also must resize to be conformable with what size the textview will be.
Thank you.
Without auto layout it is much easier. Just get the contentSize property of the UITextView:
float textViewHeight = textView.contentSize.height;
And then set scroll view content size according to the content size of the text view:
scrollview.contentSize = CGSizeMake(320,textViewHeight + 100);
This will make the height of the scroll view the height of the content size of the textview plus 100 pixels (in case you have other stuff in your scroll view).

problems with scrollview, not scrolling full length

I have a scroll view, added in the IB, and also I added a lot of objects/controls to it on IB itself. Some of them are added to the scrollview but they are out of its bounds.
Back in the class, I'm setting its content size like this
self.scrollView.delegate = self;
self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height + 500);
The scrolling length I observe is not that much. I just observe like 500 px (while the height I set in the content size is around 900 px).
Please tell me what I'm doing wrong.
I use the IB to set the scroll view and the inner controls and I let the scroll view to be longer than the view area it self as why do you need to set it inside the code unless you draw the inner controls dynamically at the runtime ...I hope it is useful

How to resize UILabel text after zooming UIScrollview zoomable

I have an UIScrollview that is zoomable, the subview is one UIView (viewTexto) that contains an UILabel inside (messageLabel).
This is the code
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollViewtmp{
return viewTexto;
}
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollViewtmp withView:(UIView *)view atScale:(float)scale{
messageLabel.contentScaleFactor=scale;
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, messageLabel.frame.origin.y + messageLabel.frame.size.heightt)];
}
With this code I can zoom, and the text is not blurry, there is no horizontal scroll but the size of the UILabel continues to be too large, so it is cut. I need that the width of the UILabel adopts to the scrollView width again as at the beginning.
I have read any question about UIScrollViews in SO and having found exactly what I need.
OK, so finally I found the answer as Ismael suggested I had to adjust the width, the problem was to find the equation.
The way the scaling works and the width of the scrollview's subviews is not obvious at the beginning.
Once you scale a UIView in a scrollview, and you want to have the same width for it, you have to divide the width of the element by the scale.
That is to say that if your initial width was 600, once you scale you can think that the width has changed and you only have to resize it again to 600, but it is not true. 600 is going to be multiplied by the scale automatically. So the right answer would be to resize it to 600 / scale.
Here we divide.
Now the code:
Everything happens in the method:
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollViewtmp withView:(UIView *)view atScale:(float)scale{}
The first thing was to get rid of the blurry fonts:
messageLabel.contentScaleFactor=scale;
In another method I saved the initial width of the UILabel messageLabel (inside scrollview), I called the variable "initialWidth", that was 600. This is important, because I started using the current messageLabel width once in the scrollViewDidEndZooming method.
To readjust the width of the subviews of the scrollview to 600, we only have to divide the initial width by the zoomscale, and readjust the label:
[messageLabel setFrame:CGRectMake(0,0,(initialWidth/scale), messageLabel.frame.size.height)];
[messageLabel sizeToFit];
At this point, we have a scrollview that can be zoomed, with a Label that readjust the text to the initial width of the scrollview, we don't have horizontal scrollbars, but we have a problem, the vertical scrollbars have a wrong height: we can only scroll a part of the text.
This was a difficult second problem to solve.
If you pass the messageLabel height to the contentsize, curiously it seems that it doesn'work, and even if I get a height multiplied by the scale in my NSLogs, it does not change the height of the scrolling, as if internally it was divided again. For example, if the initial height was 500, after scaling by 2, I get 1000 height, if I pass this value to the ContentSize it remains the same, as if it was divided again by 2.
So the solution was this time to multiply by the scale.
We only have to add this line:
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, (messageLabel.frame.origin.y+messageLabel.frame.size.height)*scale)];
As it is seen, the difficult part was to understand this mess with dividing or multiplying by the scale.
After you zoom, the contentSize of a UIScrollView increases proportionally (originalContentSize * zoomScale), so when you adjust it again, it will become smaller
Example:
Your scrollView.frame is (0, 0, 100, 100), and your contentSize is (100, 100).
After you zoom to 2.0, your contentSize will be (200, 200).
You are then putting it at (100, 100) manually, which equals to a contentSize of (50, 50) without zoom.
Since (contentSize.width <= scrollView.frame.size.width), there will be no horizontal scrolling, and the right-most part of your label is not visible.
Try not adjusting the contentSize. The scrolling will be possible then, and the user can reach the whole of the label
Edit: Re-reading your question, if you want the label to adjust and become visible entirely after the zoom, what you have to do in addition to adjusting the contentSize, is adjusting the messageLabel's frame. The label should wrap itself properly

Scrolling Issues in IOS

Scroll view did not set content size if I set it using a dynamic variable i.e.
scrollView.contentSize=CGSizeMake(320,scrollView.frame.origin.y+120);
the scroll view size remains same after calling above method.
But if I set it using a number (integer or float) it gets changed but it gives a very strange effect on scrolling. Picture of effect is attached.
scrollView.contentSize=CGSizeMake(320,1000);
This is not allowing me to post image anyways a strange effect appears.
How I can get rid of it?
Please tell Whats going wrong or what I am missing?
Now i have got rannking upto 11 the image for effect is given here.
Before Increasing Content Size:
After Increasing Content Size on Scrolling follwing effect appears:
Select the Scroll View and view its Size Inspector window. Observe that its size is 320 , 713 points (in my case). If you do not see the same
size as what I have, this is a good time to adjust the size so that it is the same as mine. You will
need to use this value in your code
(void)viewDidLoad {
scrollView.frame = CGRectMake(0, 0, 320, 460);
[scrollView setContentSize:CGSizeMake(320, 713)];
[super viewDidLoad];
}
i think you should use frame.height to achieve your goal
the content size must be greater then frame size of scroll view for scroll
and you are using y origin of scroll view so may be that's the problem.
scrollView.contentSize=CGSizeMake(320,scrollView.frame.size.height+120);
In this line:
scrollView.contentSize=CGSizeMake(320,scrollView.frame.origin.y+120);
You are setting the scroll view's contentsize's height to scrollView.frame.origin.y+120.
Means, if your scroll view's frame is x=40, y=40, width = 500, height = 500. Then the scrollView.frame.origin.y+120 will be equal to 40 + 120 = 160. Which is less than your scrollview's height. So your scroll view won't scroll to up or down.
In the second line:
scrollView.contentSize=CGSizeMake(320,1000);
You are scroll view's content size's height to 1000. That is greater than the scroll view's height so it'll scroll to up and down.
Finally.......
The issue was scroll view increases width of scroll view indicator which shows on the screnn while scrolling. You have two options to get rid of this.
Disable vertical or horizotnal indicators which one causing problem, but this may cause some problem with scroll view content size in my case it caused a problem last cell was not being viewed.
2.Let it appear but make it out of screen by the following code snippet.
[myScrollView setScrollIndicatorInsets:UIEdgeInsetsMake(0.0, 0.0, 0, 320.0)];
this is working perfectly but scroll indicator is gone out of screen.

Resources