【发布时间】:2015-10-27 16:43:47
【问题描述】:
在我的应用程序中,我想要一个带有不同地址卡的水平滚动视图,如下图所示。
我通过情节提要向其视图控制器添加了滚动视图及其内容视图。以下是限制条件。
我还为地址卡创建了一个 .xib 文件。每当我需要地址卡时,我都会通过 loadNibNamed api 加载这个 .xib,因为我不知道我需要多少地址卡。它是动态的。下面是我添加的代码。
self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
RVXVoterView *voterView = [[NSBundle mainBundle] loadNibNamed:@"RVXVoterView" owner:nil options:nil][0];
[self.contentView addSubview:voterView];
voterView.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *views = NSDictionaryOfVariableBindings(self.scrollView, self.contentView, voterView);
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-30-[voterView]-30-|" options:0 metrics:nil views:views]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-15-[voterView]-15-|" options:0 metrics:nil views:views]];
RVXVoterView *voterView1 = [[NSBundle mainBundle] loadNibNamed:@"RVXVoterView" owner:nil options:nil][0];
[self.contentView addSubview:voterView1];
voterView1.translatesAutoresizingMaskIntoConstraints = NO;
views = NSDictionaryOfVariableBindings(self.scrollView, self.contentView, voterView, voterView1);
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-15-[voterView1]-15-|" options:0 metrics:nil views:views]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:voterView1
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:voterView
attribute:NSLayoutAttributeWidth
multiplier:1
constant:0]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[voterView]-15-[voterView1]" options:0 metrics:nil views:views]];
[self.contentView layoutIfNeeded];
self.scrollView.contentSize = self.contentView.frame.size;
RVXLOG(@"content size is : %@",NSStringFromCGRect(self.contentView.frame));
[self.scrollView layoutIfNeeded];
在上面的代码中,我只添加了 2 张卡作为初始设置。 下面是我得到的最终屏幕。但我的问题是,为什么它不能滚动。我检查了所有阻止滚动的主要内容。我错过了什么。请帮我。
更新:我发现,即使我添加了一张新卡片,contentView 的宽度也没有增加。最初,contentView 的宽度等于屏幕宽度。当我添加一张新卡时,它应该增加新卡的宽度。但这并没有发生。它总是显示相同的宽度(iPhone6 为 375)。
【问题讨论】:
-
运行时滚动视图宽度和内容视图宽度的值是多少?如果它们相同,那么当您更改滚动视图的内容大小时,只需添加更多数量即可。像 10 或 20 一样,它将是可滚动的。
标签: ios uiscrollview autolayout