UIScrollView をメイン ビューに動的に追加し、小さな UIView オブジェクトで埋めています。contentSize をデバイスの解像度よりも大きく設定していますが、デバイスの向きを変更するまでスクロールできません。
向きを変更する前にスクロールするにはどうすればよいですか?
後で編集:
問題は、追加している特定のサブビュー、より具体的には、それらのサブビューにいくつかの制約を追加することから発生することに気付きました:
サブビューを追加するコードは次のとおりです (メソッドは約 10 ~ 20 回呼び出されます)。
-(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 は UIView のサブクラスであり、shelfView は UIScrollView のサブクラスです。
で追加された制約は次の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]];
制約を追加するための FSUtils クラスのメソッド:
+ (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;
}
これらの制約に何か問題があることに気づきましたか?