How do I disable horizontal scroll in UIScrollView on an iPad? - ipad

I only want to activate vertical but not horizontal scrolling. How can I achieve this in the UIScrollView class?

Make use of contentSize property of the UIScrollView.
For example, if your scrollView size is 260 pixels wide,then
CGSize scrollArea = CGSizeMake(260, scrollableHeight); //you can use yourScrollView.width instead of 260 if you assigned its frame already.
[yourScrollView setContentSize: scrollArea];

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 );

how make the UIView height equal to its subview's additional height, so that it can easily scroll till the end

I have a situation where i have following view layout.
-UIViewcontroller
-SCrollView
-UIView(outer)
-buttons
-labels
-UIView(inner)
-labels
-buttons
-buttons
the inner UIView height can be any longer as content inside is dynamically added.
So the main issue is i can not scroll till the end of the content, instead i can only scroll till the height of inner UIView.
NOTE - I am using auto layout
Any help is much appreciated.
Thanks
You should change the frame of your inner view dynamically based on the content you are adding. According to your inner view hierarchy, you have label and button. You might be setting some dynamic text on label and also doing some manipulation with button . Use the following method to get the height of the text ,
- (CGSize)getHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
CGSize size = CGSizeZero;
if (text) {
CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{ NSFontAttributeName:font } context:nil];
size = CGSizeMake(frame.size.width, frame.size.height);
}
return size;
}
Here font is the font you set to button and label. Call this method by passing the text added on label and button. CGSize has a height property , add height value of label and button. Now set this sum height to innerView by calling innerView.frame.size.height = <height>. This increase the height of your innerView based on content inside that.
Put all your buttons in the bottom inside a container view and add the following constraints to the outerView (assuming that the new container you've added is called "buttonContainer"):
A vertical spacing constraint between the bottom of innerView and the top of the buttonContainer.
A vertical spacing constraint between the bottom of the buttonContainer and the bottom of the outerView.
Horizontal spacing constraints between between the buttonContainer and outerView to force it to full width.
When using AutoLayout, you have to have enough constraints to properly define the contentSize of the scrollView.
For more information about using AutoLayout with UIScrollView, check out the following links by Apple:
Working with Scroll Views
UIScrollView and Autolayout

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).

iOS UIScrollView size child view only once

I have an image as a subview of a UIScrollView. I want the image to initially fit the screen's bounds using autoResizingMask and contentMode (UIViewContentModeAspectFit) property of UIView. When the scrollview's frame is changed to make room for the keyboard, I don't want the child image view to scale down to fit the smaller frame. I can't disable autoResizeSubviews on the scroll view when it is created because the child view must be re-sized once at the beginning. Right now I can turn off subview re-sizing when the keyboard appears and re-enable it when it dissapears. This seems to work fine but seems hackish. Is that an acceptable way to do it or is there a better solution? Thanks.
Don't worry about the autoResizingMask since it's a subview of the UIScrollView.
The autoResizingMask on a UIView allows for the view to be automatically resized when its parent view's frame has been resized. In this scenario, it sounds like your scrollView's frame is being adjusted vertically to accommodate the keyboard's on screen frame. When the parent view's frame shrinks, so does your UIImageView, which means it works as designed if you're using UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight as the autoResizingMask of your UIImageView.
Instead of using autoResizingMask on your UIImageView, you should manually set its frame to be the same size as the UIScrollView's frame via the bounds property:
imageView.frame = scrollView.bounds;
Then let's set the scrollView's contentSize to be the same size as the imageView.
scrollView.contentSize = imageView.frame.size;
This way, your imageView should be the full size of your scrollView, and won't move if you adjust the size of the scrollView frame.

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.

Resources