OGeek|极客世界-中国程序员成长平台

标题: ios - 具有水平滚动的 UIScrollView,具有自动布局的分页 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:36
标题: ios - 具有水平滚动的 UIScrollView,具有自动布局的分页

在我的应用程序中,我想要一个带有不同地址卡的水平 ScrollView ,如下图所示。

Address Cards

我通过 Storyboard 向其 View Controller 添加了 ScrollView 及其内容 View 。以下是限制条件。

Autolayout Tree

我还为地址卡创建了一个 .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 张卡作为初始设置。 下面是我得到的最终屏幕。但我的问题是,为什么它不能滚动。我检查了所有阻止滚动的主要内容。我错过了什么。请帮我。 Final result

更新:我发现,即使我添加了新卡片,contentView 的宽度也没有增加。最初,contentView 的宽度等于屏幕宽度。当我添加一张新卡时,它应该增加新卡的宽度。但这并没有发生。它总是显示相同的宽度(iPhone6 为 375)。



Best Answer-推荐答案


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

首先像您在演示的代码中所做的那样,为 subview 添加 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]];

关于ios - 具有水平滚动的 UIScrollView,具有自动布局的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31823494/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4