UIScrollView does not scroll until orientation changes

I have a UIScrollView dynamically added to the main view, and I populate it with small UIView objects. I set its contentSize to something more than the resolution of the device, but I cannot scroll it until I change the orientation of the device.

How can I scroll before changing the orientation?

LATER EDIT:

I realized that the problem arises from some subheadings that I add, or rather, from adding some limitations to these views:

Here is the code where I add 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 restrictions added to 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]];

Methods from the FSUtils class to add contraindications:

+ (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;
}

- ?

+5
4

Tap,

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

. , scrollView.

, 100 * 100, 100 * 100. frame.width < contentize.width, . , frame.height < contentize.height, .

, , , , , , scrollview.frame , . , , .

, .

+1

, shelfView ( , UIScrollView), . x origin FSShelf *shelf = [[FSShelf alloc] initWithFrame:CGRectMake(0, index * [FSConstants getShelfVerticalDistance], 0, 0)];, , - ( ), x, subview, - x! , , . , , - . , , . - ?

, : shelfView - . , , , , , .

. , , . , Shelb ( , , , ), , , . , , - [self.view setNeedsLayout];, viewDidLayoutSubviews . - :

- (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);
}
+1

, , . -. . , .
, Andrea

[EDIT]
a >= iOS6, . , , , iOS >= 4.3. , . github

0

All Articles