UIScrollView doesn't work - ios

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

Related

UICollectionView - Scroll out of bounds

I have an UICollectionView let's say 200 width. I can scroll vertically without any problems. But I would like to be able to scroll the collectionView 60 px out of bounds to the right. Is this possible?
Sure it is, UICollectionView inherits from UIScrollView, that means that you can add an inset. This means that you will scroll more but the content size where you place your cell will be the same.
SWIFT var contentInset: UIEdgeInsets OBJECTIVE-C
#property(nonatomic) UIEdgeInsets contentInset Use this property to add to the
scrolling area around the content. The unit of size is points. The
default value is UIEdgeInsetsZero.
Remember also to add a scrollIndicatorInsets to balance the scroll bar movement.

UIView in UIScrollView does not adjust width

I have been searching through stackOverflow and whatever google proposes but I wasn't able to get it to work. I am intending to draw a simple 2D Graph in a scrollview, the distance between my datapoints is kStepX and I want the scrollview to be at least the width of the screen, if I have more datapoints it should scroll but no more than 100 points.
I think I have a problem with my Autolayout and sizing the contentWidth, so here is what I have done so far:
I added a UIScrollView with the following constraints:
Leading Space to Superview =0
Top space to superview =0
Height = width of superview
Width = width of superview
I then added a UIView (called GraphView) as a Child with the following constraints:
zero space to all 4 bounds of scrollview
center X and center Y to scrollview
in my GraphViewController I set the contenSize as:
historyScrollView.contentSize = CGSizeMake(MAX(SCREEN_WIDTH,sizeof(data)*kStepX), kGraphHeight);
but it does not scroll!
if I set a fix width in the storyboard the scrollview scrolls further than I have data..
What am I doing wrong?
You should not be setting the contentSize of the scrollView when using auto layout. That should be calculated from your constraints if they are setup correctly.
What you are missing is to set a width and height constraints on the view inside the scrollView. A scrollView determines it's contentSize based on the size the subviews have. Since an UIView does not have an intrinsic size, you will need to add width and height constraints to it, then update it when you need it with the right value
Something like this should work:
innerViewWidthConstraint.constant = MAX(SCREEN_WIDTH,sizeof(data)*kStepX)
innerViewHeightConstraint.constant = kGraphHeight
// You might need to layout the views too
[historyScrollView layoutIfNeeded]
Hope this helps! Good luck :)
Take ScrollView in side in your Main View and give Top,Bottom,Leading,Trailing,Center X and Center Y.
after take GraphView inside you scrollview and also set constrain of GraphView.
set height of GraphView
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *HeightGraphView;
whenEver you set your HeightGraphView then automatically set ScrollView contentSize.
see my demo its working for Vertically scrolling, apply same for Horizontal scrolling.
Demo Vertical Scrolling

How can I lay views out on a xib's UIScrollView?

I have a view that has a horizontally scrolling UIScrollView. I have 4 "pages" that I want to show in my scroll view. Can I layout a huge view in Interface Builder that I can scroll across? The problem I'm having while trying to do this is that to design a xib this large across horizontally, you must set the frame of the UIScrollView to be the devices horizontal width * 4. Therefore the frame >= the content size and my scroll view does not scroll. My workaround has been to place all the elements on each of the 4 pages programmatically and adding them to my UIScrollView with addSubView: What are my other options?
You keep the frame of scroll view as horizontal width only. You should make the content size as horizontal width * 4. Now set proper frame of the views which you want to put as subviews of that scroll view. Like first subview's frame should have origin.x = horizontal width * 0, second should have origin.x = horizontal width * 1, third should have origin.x = horizontal width * 2 and fourth should have origin.x = horizontal width * 3.
This will eliminate the need of putting subviews programatically.
Freeform your xib, design your view

Scroll UITableView horizontally using auto layout in storyboard

I used storyboard with autolayout enable. I put a UIScrollView in storyboard. Then drag a UITableView over the scrollview. I set the table view's width bigger than the scroll view's width. I then set the constraints with the trailing space to scrollview with a negative number and storyboard fill all other constraints without error. The structure look like:
UIView
UIScrollView
UITableView
in code, I set the scroll view content size:
self.scrollView.contentSize = CGSizeMake(500, 400);
the size matches table view's size. But it doesn't work. The table view will not scroll horizontally. What I am doing wrong and how to make a UITableView scroll horizontally. I mean the whole table, not the each cell scroll horizontally. Thanks for your help.

UIScrollView contentSize height equal its frame height, why there is still a scroll bar?

I config scrollView contentSize same as its frame height(I also test <), Why scorll bar still shown, it seems that the scrollview content size is not what I configed in code.
I think you are place scroll view in view with size for Retina 4 inch, and scroll view automaticaly resized.
add line in viewDidLoad method
self.mainScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
or set up right autoresizing mask in IB.

Resources