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?
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With