12

How do I set the starting point of a UIScrollView? I would like to add a UIImageView left of the UIScrollView but changing the contentSize only adds scrolling room to the right of the scrollview. How do I add an ImageView left of the scrollView's (0,0) point and make it part of the scrollview's content size?

gurooj
  • 2,060
  • 4
  • 21
  • 25
  • 1
    How did you solve that problem where you get extra space added onto right instead of being started from left? Like, the case you have mentioned where you start contentOffset from (-320,0) and increase the size by 320px. Ideally the extra space should be added to the left and not right. I too am facing exactly the same problem but the difference is that I am performing a vertical scroll. Please let me know how you could solve this problem? – Raj Pawan Gumdal Oct 04 '11 at 12:59
  • I too have added a similar new question since there are no answers, please see if you can answer it: http://stackoverflow.com/questions/7648492/uiscrollview-setting-proper-contentoffset-for-new-contentsize-yields-undesirabl – Raj Pawan Gumdal Oct 04 '11 at 13:25

5 Answers5

9

Hopefully I've got what you're trying to do here. I think this just takes a few turns with the contentOffset to get right.

Starting off;

  • Add the scrollView at frame (0,0,320,480) - its a full screen scroller
  • set contentSize to (320*3, 480) - it now has a content with the width of 3 'pages'
  • Add your imageView as a subview to the scrollView at frame (320,0,320,480)
  • set contentOffset of the scrollView to (320, 0) - this will move the content of the scrollView left, in the negative x direction by 320
  • Now your imageView will be on screen, but it will have a 320px width both on the left and right on the scroller content.

(Note that in the code below, i've simply added a UIView and not an imageView)

UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroller.delegate = self;
scroller.pagingEnabled = YES;
scroller.backgroundColor = [UIColor blueColor];
scroller.contentSize = CGSizeMake(960, 480);

UIView *imgView = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 480)];
[imgView setBackgroundColor:[UIColor redColor]];
[scroller addSubview:imgView];

[scroller setContentOffset:CGPointMake(320, 0)];
[self.view addSubview:scroller];

Does that help?

Madhu
  • 2,439
  • 14
  • 31
  • I ended up doing this on my own, but for anyone who comes upon this question, this is the solution I used. Thanks for your help. – gurooj Oct 10 '11 at 20:27
  • 1
    Also want to point out that it is extremely important that you set the delegate for the UIScrollView or else it won't reposition the view. – gurooj Mar 16 '13 at 05:46
3

Actually the best solution to start in the middle of the scroller if you are in a iPhone you should

[scroller setContentOffset:CGPointMake(320, 0)];
[self.view addSubview:scroller];

And for iPad

[scroller setContentOffset:CGPointMake(1024, 0)];
[self.view addSubview:scroller];
Amir Ahmed
  • 31
  • 1
1

Try setting the Content offset.

[scrollView setContentOffset:CGPointMake(320, 0.0)];
0

The key to setting the starting point, is to assign bounds with xy coordinates different than zero. Like this

lazy var contentView: UIView = {
    let size = CGFloat(5000)
    let view = UIView(frame: CGRectZero)
    view.frame = CGRectMake(0, 0, size, size)
    view.bounds = CGRectMake(-size/2, -size/2, size, size)
    return view
}()

func centerContent() {
    let frame = UIEdgeInsetsInsetRect(self.scrollView.bounds, self.scrollView.contentInset)
    let navigationBarHeight = self.scrollView.contentInset.top
    let x = (self.scrollView.contentSize.width/2) - (frame.size.width/2)
    let y = (self.scrollView.contentSize.height/2) - (frame.size.height/2) - navigationBarHeight
    self.scrollView.contentOffset = CGPointMake(x, y)
}
neoneye
  • 44,507
  • 23
  • 155
  • 143
0

If you're simply looking to set where the scroller content should be positioned at the start, you could do so with the - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated method which will scroll the content to the point specified in the method.

Let me know if this is not what you wanted to know, and I'll get back to ya!

Cheers.

Madhu
  • 2,439
  • 14
  • 31
  • I was hoping that I could allow the scroller to move left without moving the scrollview. Currently, when I change the contentSize, it only adds extra scroll area to the right, how do I add scroll room to the left without moving the scrollview itself? – gurooj Sep 06 '11 at 10:35
  • Hi Gurook, just to understand the issue better; do you need to programmatically move the **content** of the scrollview left? Do you need the content of the scrollview to be offset to the right when the scrollview comes on screen (i.e. there is blank space on the scroller on the left of the actual visible content you need to show) – Madhu Sep 06 '11 at 10:49
  • I would rather the content of the scrollview be offset to the right. – gurooj Sep 06 '11 at 10:56
  • 1
    Well, lets say you're adding a UIView as the content of the scroller that you need the user to see, simply setting the x value on its frame as you add it to the subview will position it where you need it. e.g. if you set the frame as CGRect(50, 0, 320, 480); this will create a 320x480 image positioned 50 pixels to the right on the scrollview. There will be 50px worth of blank space on the left now. – Madhu Sep 06 '11 at 11:03
  • 1
    That's not really what I'm looking for. In my case, I'm adding an imageview with a width of 320 pixels to the left of the scrollview. So it will be at (-320,0). Then I increase the content size by 320. But that only adds scroll size to the right. How do allow the scroller to move left into the negative x direction? – gurooj Sep 06 '11 at 11:10
  • Ho gurooj, added a new answer, check it out. – Madhu Sep 07 '11 at 10:30
  • Gurooj & @MadhumalGunetileke - Please check my comment to the question above. – Raj Pawan Gumdal Oct 04 '11 at 13:00