• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 具有水平滚动的 UIScrollView,具有自动布局的分页

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:36:40 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

在我的应用程序中,我想要一个带有不同地址卡的水平 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap