【问题标题】:UIScrollView with horizontal scrolling, paging with autolayout具有水平滚动的 UIScrollView,具有自动布局的分页
【发布时间】: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


【解决方案1】:

要使用 AutoLayout 使 UIScrollView 滚动,您必须确保所有子视图都绑定到滚动视图的所有四个边缘。从您的代码中,我可以看到您将第一个视图(voterView)绑定到 contentView 的所有四个边缘,并尝试将第二个视图(voterView1)绑定到第一个视图的右侧,我认为这不会起作用第一个视图的右侧已经绑定到它的父视图(contentView)。

首先为子视图添加 widthheight 约束,就像您在演示的代码中所做的那样,然后尝试以下 VFL:

NSDictionary *views = NSDictionaryOfVariableBindings(voterView);
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-15-[voterView]-15-|" options:0 metrics:nil views:views]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-15-[voterView]" options:0 metrics:nil views:views]];

views = NSDictionaryOfVariableBindings(voterView, voterView1);
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-15-[voterView]-15-|" options:0 metrics:nil views:views]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[voterView]-15-[voterView1]-15-|" options:0 metrics:nil views:views]];

【讨论】:

  • 非常感谢您的时间和患者。但不幸的是,添加您提到的更改后没有任何改进。
  • 这可能是由我不知道的其他一些问题引起的。我编写了一个有用的类,可以更轻松地使用 UIScrollView + AutoLayout,您可能需要查看演示,看看它是否适合您的需求。 github.com/neevek/Easy-UIScrollView-with-AutoLayout
猜你喜欢
  • 2012-06-15
  • 2013-10-27
  • 1970-01-01
  • 2015-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-12
相关资源
最近更新 更多