Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView not scrolling until orientation changes

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?

like image 916
gabitzish Avatar asked Oct 21 '22 14:10

gabitzish


1 Answers

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];
like image 86
Vedchi Avatar answered Oct 24 '22 03:10

Vedchi