Reputation: 211
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:
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?
Upvotes: 1
Views: 1270
Reputation: 3494
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 :)
Upvotes: 1
Reputation: 23407
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.
Upvotes: 0