5

I have a UIScrollView dynamically added into the main view, and I'm filling it with small UIView objects. I'm setting it's contentSize to something bigger than the device's resolution but I can't scroll it until I'm changing the device's orientation.

How can I make it scroll before orientation change ?

LATER EDIT :

I realized that the problem comes from certain subviews that i'm adding, more specifically from adding some contraints for those subviews:

Here is the code where I'm adding the subviews (the method is called about 10-20 times) :

-(void) addShelve:(NSInteger) index
{
FSShelf *shelf = [[FSShelf alloc] initWithFrame:CGRectMake(0, index * [FSConstants getShelfVerticalDistance], 0, 0)];
[shelves addObject:shelf];

[shelfView addSubview:shelf];
[shelf addShelfConstraints];
}

FSShelf is a subclass of UIView and shelfView is my subclass of UIScrollView.

And here are the constraints added at addShelfContraints :

    [self.superview addConstraint:[FSUtils getWidthConstraintFor:self.superview with:self constant: -2 * ([FSConstants getShelveMargin])]];
    [self addConstraint:[FSUtils getSelfHeightConstraintFor:self constant:30]];
    [self.superview addConstraint:[FSUtils getCenterXConstraintFor:self.superview with:self constant:0]];

The methods from the FSUtils class for adding contraints:

+ (NSLayoutConstraint *)getSelfHeightConstraintFor:(UIView *)view constant:(CGFloat)constant
{
    NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:view
                                                                  attribute:NSLayoutAttributeHeight
                                                                  relatedBy:NSLayoutRelationEqual
                                                                     toItem:nil
                                                                  attribute:NSLayoutAttributeNotAnAttribute
                                                                 multiplier:1.0
                                                                   constant:constant];
    return constraint;
}

+ (NSLayoutConstraint *)getCenterXConstraintFor:(UIView *)superView with:(UIView *)subView constant:(CGFloat)constant
{
    NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:subView
                                                                  attribute:NSLayoutAttributeCenterX
                                                                  relatedBy:NSLayoutRelationEqual
                                                                     toItem:superView
                                                                  attribute:NSLayoutAttributeCenterX
                                                                 multiplier:1.0
                                                                   constant:constant];

    return constraint;
}

+ (NSLayoutConstraint *)getWidthConstraintFor:(UIView *)superView with:(UIView *)subView constant:(CGFloat)constant
{
    NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:subView
                                                                  attribute:NSLayoutAttributeWidth
                                                                  relatedBy:NSLayoutRelationEqual
                                                                     toItem:superView
                                                                  attribute:NSLayoutAttributeWidth
                                                                 multiplier:1.0
                                                                   constant:constant];
    return constraint;
}

Do you notice anything wrong with those contraints?

5
  • 2
    Can you give us some extra details on your setup? When is the UIScrollView initialized? Is the scroll view's frame smaller than its contentSize (before rotation)? The easiest way I can imagine to hit your bug is for the scroll view's frame to be too large. Mar 21, 2013 at 17:59
  • The scrollView has constraints to be 100% width and (100% - 50px height). I checked the content size and it's height is bigger than 2000px. I'm initializind the UIScrollView on a singleTap event, when I'm replacing the content of the main view (removing a uiview and adding the scrollview). I'll post some code later today when I get back to the project.
    – gabitzish
    Mar 22, 2013 at 11:03
  • please post your code related to your UIScrollView.
    – Hiren
    Mar 22, 2013 at 12:42
  • Could we see code please or/and the steps you did to create the scrollView in interfaceBuilder.
    – BooRanger
    Mar 22, 2013 at 14:54
  • @gabitzish You should provide some code to know what you're doing. Mar 23, 2013 at 4:30

4 Answers 4

1

On your single Tap, try to create like this

UIScrollView * scrollview = [[UIScrollView alloc]init];
CGRect viewFrame = self.view.frame;
[scrollview setFrame:viewFrame];
[scrollview setShowsHorizontalScrollIndicator:YES];
[scrollview setShowsVerticalScrollIndicator:YES];

UIView * view = [[UIView alloc]init];
CGRect viewDoubleFrame = CGRectMake(viewFrame.origin.x,viewFrame.origin.y,viewFrame.size.width * 2,viewFrame.size.height *2);
[view setFrame:viewDoubleFrame];
[view setBackgroundColor:[UIColor darkGrayColor]];

// add your additional components here

[scrollview setContentSize:CGSizeMake(view.frame.size.width,view.frame.size.height)];
[scrollview addSubview:view];

[self.view addSubview:scrollview];
1

Scrolling is not having any relation with Device Resolution. Scroll occurs when the size of your content are greater than the size of your scrollView frame.

In general to make the scroll possible for content having size 100*100, you should have a scroll view having frame size less than 100*100. If the frame.width < contentsize.width, it will allow horizontal scrolling. In the same way if frame.height < contentsize.height, it will allow vertical scrolling.

You may have selected auto layout or you might have used auto resizing flags without care which may lead to have the size of your scrollview.frame greater than the contentsize. When you provide orientation the frame may get settled perfectly an you are able to scroll.

Post the code if you want to spot the exact problem.

1

The constraints you are attempting to place on shelfView (which I assume is your UIScrollView) are not sensible. You are creating shelf subviews with zero width and zero x origin FSShelf *shelf = [[FSShelf alloc] initWithFrame:CGRectMake(0, index * [FSConstants getShelfVerticalDistance], 0, 0)]; and then immediately trying to constrain the shelfView to have that same zero width minus some constant (so shelfView would need negative width) and then the same center x as the subview, but the shelf view is still zero width and has zero x origin! The only constraint you're assigning to the shelf subviews are on their height. In fact, I think something is missing here. With the code you've posted your shelf subviews always have zero width. Do you actually see any of the shelf subviews on screen?

Anyway, here's my advice: Trying to add constraints to shelfView based on each of its shelf subviews is a losing proposition. In this case the constraints appear to be unsatisfiable, and in general you will find that various constraints become unsatisfiable unless you are very careful to ensure that all the subviews always agree on exactly how their superview ought to be laid out.

Instead of all this, just forget the constraints code. What you're trying to do is very simple. The shelfView is supposed to have a frame equal to the size of the screen (or the application frame, anyway probably the bounds of its superview) and the shelf views are supposed to be arranged vertically with some vertical margins between them and some horizontal margins separating them from the edge of the screen. Add the views like you are now, but instead of adding any constraints call [self.view setNeedsLayout]; and then implement the viewDidLayoutSubviews method in your view controller. You'll want something like this:

- (void)viewDidLayoutSubviews {
    const CGRect rootViewBounds = self.view.bounds;
    shelfView.frame = rootViewBounds;

    const CGFloat kHorizontalMargin = [FSConstants getShelveMargin];
    const CGFloat kVerticalDistancePerShelf = [FSConstants getShelfVerticalDistance];
    const CGFloat kShelfHeight = 30.0;
    const NSUInteger shelvesCount = [shelves count];
    for (NSUInteger j = 0; j < shelvesCount; j++) {
        UIView *shelf = [shelves objectAtIndex:j];
        shelf.frame = CGRectMake(kHorizontalMargin,
                                 j * kVerticalDistancePerShelf,
                                 CGRectGetWidth(rootViewBounds) - 2.0 * kHorizontalMargin,
                                 kShelfHeight);
    }
    shelfView.contentSize = CGSizeMake(CGRectGetWidth(rootViewBounds), (shelvesCount - 1) * kVerticalDistancePerShelf + kShelfHeight);
}
0

Pay attention that if you are using auto layout, just changing content size is not enough. Scroll View are layouted in a different way. I've found very helpful this question on stack Here. So check if you are using auto layout and adapt to it or disable it.
Hope this helps, Andrea

[EDIT]
If you target devices >=iOS6, why don't you use collection views. They are pretty cool stuff, with awewsome layout features and thank to Peter you have a drop in replacement for iOS>=4.3. For what I understood they could fit your requirement. github

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.