Set starting point of content size for UIScrollView
Asked Answered
E

5

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?

Ellipticity answered 6/9, 2011 at 10:21 Comment(2)
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?Constriction
I too have added a similar new question since there are no answers, please see if you can answer it: #7648992Constriction
B
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?

Bimah answered 7/9, 2011 at 10:30 Comment(2)
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.Ellipticity
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.Ellipticity
C
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];
Cleistogamy answered 14/6, 2012 at 18:2 Comment(0)
B
1

Try setting the Content offset.

[scrollView setContentOffset:CGPointMake(320, 0.0)];
Bryson answered 27/1, 2015 at 11:54 Comment(0)
B
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.

Bimah answered 6/9, 2011 at 10:29 Comment(7)
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?Ellipticity
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)Bimah
I would rather the content of the scrollview be offset to the right.Ellipticity
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.Bimah
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?Ellipticity
Ho gurooj, added a new answer, check it out.Bimah
Gurooj & @MadhumalGunetileke - Please check my comment to the question above.Constriction
I
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)
}
Ido answered 3/8, 2014 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.