gpt4 book ai didi

ios - UIScrollView 在方向改变之前不滚动

转载 作者:可可西里 更新时间:2023-11-01 04:44:21 25 4
gpt4 key购买 nike

我有一个 UIScrollView 动态添加到主视图中,我用小的 UIView 对象填充它。我将其 contentSize 设置为大于设备分辨率的值,但在更改设备方向之前我无法滚动它。

如何让它在方向改变之前滚动?

后期编辑:

我意识到问题来 self 正在添加的某些 subview ,更具体地说是来自为这些 subview 添加一些约束:

这是我添加 subview 的代码(该方法被调用了大约 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;
}

你注意到这些约束有什么问题吗?

最佳答案

在您的单次点击中,尝试像这样创建

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];

关于ios - UIScrollView 在方向改变之前不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15506347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com