菜鸟教程小白 发表于 2022-12-13 09:36:40

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


                                            <p><p>在我的应用程序中,我想要一个带有不同地址卡的水平 ScrollView ,如下图所示。</p>
<p> <a href="/image/f2Zbd.png" rel="noreferrer noopener nofollow"><img src="/image/f2Zbd.png" alt="Address Cards"/></a> </p>
<p>我通过 Storyboard 向其 ViewController 添加了 ScrollView 及其内容 View 。以下是限制条件。</p>
<p> <a href="/image/Er8vr.png" rel="noreferrer noopener nofollow"><img src="/image/Er8vr.png" alt="Autolayout Tree"/></a> </p>
<p>我还为地址卡创建了一个 .xib 文件。每当我需要地址卡时,我都会通过 loadNibNamed api 加载这个 .xib,因为我不知道我需要多少地址卡。它是动态的。下面是我添加的代码。</p>
<pre><code>self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;

RVXVoterView *voterView = [ loadNibNamed:@&#34;RVXVoterView&#34; owner:nil options:nil];
;

voterView.translatesAutoresizingMaskIntoConstraints = NO;

NSDictionary *views = NSDictionaryOfVariableBindings(self.scrollView, self.contentView, voterView);
-30-|&#34; options:0 metrics:nil views:views]];
-15-|&#34; options:0 metrics:nil views:views]];

RVXVoterView *voterView1 = [ loadNibNamed:@&#34;RVXVoterView&#34; owner:nil options:nil];
;

voterView1.translatesAutoresizingMaskIntoConstraints = NO;
views = NSDictionaryOfVariableBindings(self.scrollView, self.contentView, voterView, voterView1);
-15-|&#34; options:0 metrics:nil views:views]];

[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:voterView1
                                                             attribute:NSLayoutAttributeWidth
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:voterView
                                                             attribute:NSLayoutAttributeWidth
                                                            multiplier:1
                                                            constant:0]];
-15-&#34; options:0 metrics:nil views:views]];
;
self.scrollView.contentSize = self.contentView.frame.size;
RVXLOG(@&#34;content size is : %@&#34;,NSStringFromCGRect(self.contentView.frame));
;
</code></pre>
<p>在上面的代码中,我只添加了 2 张卡作为初始设置。
下面是我得到的最终屏幕。但我的问题是,为什么它不能滚动。我检查了所有阻止滚动的主要内容。我错过了什么。请帮我。
<a href="/image/qsNUc.png" rel="noreferrer noopener nofollow"><img src="/image/qsNUc.png" alt="Final result"/></a> </p>
<p><strong>更新:</strong>我发现,即使我添加了新卡片,contentView 的宽度也没有增加。最初,contentView 的宽度等于屏幕宽度。当我添加一张新卡时,它应该增加新卡的宽度。但这并没有发生。它总是显示相同的宽度(iPhone6 为 375)。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>要使 <code>UIScrollView</code> 使用 AutoLayout 滚动,您必须确保所有 subview 都绑定(bind)到 ScrollView 的所有四个边缘。从您的代码中,我可以看到您将第一个 View (voterView)绑定(bind)到 contentView 的所有四个边缘,并尝试将第二个 View (voterView1)绑定(bind)到第一个 View 的右侧,我认为这不会起作用第一个 View 的右侧已经绑定(bind)到它的父 View (contentView)。</p>

<p>首先像您在演示的代码中所做的那样,为 subview 添加 <code>width</code> 和 <code>height</code> 约束,然后尝试以下 VFL:</p>

<pre><code>NSDictionary *views = NSDictionaryOfVariableBindings(voterView);
-15-|&#34; options:0 metrics:nil views:views]];
&#34; options:0 metrics:nil views:views]];

views = NSDictionaryOfVariableBindings(voterView, voterView1);
-15-|&#34; options:0 metrics:nil views:views]];
-15--15-|&#34; options:0 metrics:nil views:views]];
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 具有水平滚动的 UIScrollView,具有自动布局的分页,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/31823494/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/31823494/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 具有水平滚动的 UIScrollView,具有自动布局的分页