菜鸟教程小白 发表于 2022-12-12 16:28:10

ios - 无法更改自定义 View 的框架大小


                                            <p><p>我有一个自定义的 uiview。我使用 xib 来布局 subview 并添加约束来自动布局它们。吹是在IB。
<a href="/image/1FOiC.png" rel="noreferrer noopener nofollow"><img src="/image/1FOiC.png" alt="enter image description here"/></a> </p>

<p> <a href="/image/9PiaD.png" rel="noreferrer noopener nofollow"><img src="/image/9PiaD.png" alt="enter image description here"/></a> </p>

<p>现在我有一些代码覆盖了 initWithFrame: 方法。
这是代码。</p>

<pre><code>- (id)initWithFrame:(CGRect)frame {
self = ;
if (self) {
    MyCustomView *view = [ loadNibNamed:@&#34;MyCustomView&#34; owner:self options:nil].firstObject;
    ;
}
return self;
</code></pre>

<p>}</p>

<p>并且,在其他一些来源中,我使用上面定义的 initWithFrame: 方法创建它并将其添加到 UIScrollView。我使用约束让 ScrollView 的框架适合屏幕大小。代码在这里。</p>

<pre><code>CGRect contentRect = CGRectZero;
for (int index = 0;index &lt; self.valueData.count;index ++) {
    MyCustomView *cellView = [ initWithFrame:CGRectMake(0, CUSTOM_VIEW_HEIGHT * index, SCREEN_WIDTH, CUSTOM_VIEW_HEIGHT)];
    ;
    contentRect = CGRectUnion(contentRect, cellView.frame);
}
self.scrollView.contentSize = contentRect.size;
</code></pre>

<p>我在 iPhone 5s 模拟器中看到的内容:</p>

<p> <a href="/image/tNJ6Q.png" rel="noreferrer noopener nofollow"><img src="/image/tNJ6Q.png" alt="enter image description here"/></a> </p>

<p>但在 iPhone 6 模拟器中:</p>

<p> <a href="/image/lw7JR.png" rel="noreferrer noopener nofollow"><img src="/image/lw7JR.png" alt="enter image description here"/></a> </p>

<p>尽管我将框架的宽度设置为定义的 SCREEN_WIDTH,但它们并没有占据屏幕的整个宽度:</p>

<pre><code>#define SCREEN_WIDTH (.bounds.size.width)
</code></pre>

<p>如何设置这些自定义 View 的宽度以适应多个设备的屏幕宽度?</p>

<p><strong>添加:</strong>
这是自定义 View 中 UI 内容的所有约束。
<a href="/image/wLnud.png" rel="noreferrer noopener nofollow"><img src="/image/wLnud.png" alt="enter image description here"/></a> </p>

<p> <a href="/image/dveCn.png" rel="noreferrer noopener nofollow"><img src="/image/dveCn.png" alt="enter image description here"/></a> </p>

<p> <a href="/image/Vw3pS.png" rel="noreferrer noopener nofollow"><img src="/image/Vw3pS.png" alt="enter image description here"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>混合使用 Autolayout 和 setFrame 通常是一个坏主意,这会在您尝试调试时导致很多麻烦。您是否使用“捕获 View 层次结构”查看了自动生成的约束? </p>

<p>您真正想要做的不是依赖生成的约束(尤其是因为在显示之前您不能依赖 View /窗口大小)。实际上,您需要做一些非常重要的事情:</p>

<ol>
<li>不要将多个 View 直接附加到 ScrollView 。你想要<em>一个</em>
附加到 ScrollView 的 View ,以及其他所有内容的 subview
那。我通常称之为内容 View 。 </li>
<li>确保您的内容 View 正确附加到您的 ScrollView 之前
添加其他内容(无错误/警告)。 </li>
<li>插入带有约束的 AssetsView 。 0 左右,0 到
最后一个 View 。不管你是使用 constraintWithItem 还是
constraintsWithVisualFormat。</li>
<li>将内容 View 的底部绑定(bind)到最后插入的 subview ,使其具有
定义高度,删除或取消任何其他高度限制的优先级。</li>
</ol></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 无法更改自定义 View 的框架大小,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/32901182/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/32901182/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 无法更改自定义 View 的框架大小