1
votes

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:

  1. 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
  2. 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?

2

2 Answers

1
votes

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

0
votes

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