菜鸟教程小白 发表于 2022-12-13 03:44:40

ios - iphone 6 plus 上具有 600x600 pt 框架的 UIViews 以及大小类


                                            <p><p>我正在开发一个可在所有设备(iPad、iPhone)上使用的应用程序。我为 iPad 和 iPhone 平台使用不同的 xib 文件(只是因为原始应用程序仅适用于 iPad 并且效果很好(没有任何自动布局))。</p>
<p>我的 iPhone xib 文件使用自动布局和大小类。我有启动屏幕,我的启动图像设置好了。当应用程序启动时,viewDidLoad 中的 View 框架为 600x600,但没关系,在 viewWillAppear 之前我什么都不做。</p>
<p>此时 xib 文件的框架是正确的(对于 iphone5 和 iphone 6 和 iPhone 6+)。但是我在这个 xib 中几乎没有其他 View 与主视图平行(不是任何其他 View 的 subview )。所有这些其他 View 都有一个 600x600 的框架。</p>
<p>我可以显式设置框架,但我必须手动调整每个 subview 的大小...</p>
<p>如果我关闭尺寸类并仅使用自动布局,则框架始终为 iphone 5s 尺寸。</p>
<p>附:我刚刚添加了所有可能的启动图像(正确尺寸)删除了尺寸类和尺寸如下:
主视图 - 框架 = (0 0; 667 375)</p>
<p>在xib中查看旁边的(不是 subview ,但也必须是全屏 View )-frame = (0 0; 568 320)</p>
<p>所有 View 尺寸都是内部和横向的。</p>
<p><strong>编辑 1</strong>
所以我的问题是:
xib 中的其他 View 如何在正确的框架中初始化?不是 600x600。</p>
<p>如果我使用“在 View 层次结构中调试”,我可以看到除主“ View ”之外的所有作为 Root View (而不是任何 subview )的 View 的大小都不合适。即使正确报告了屏幕边界, View 仍然是 600x600 大小。</p>
<p><strong>编辑 2</strong>
这个问题提出了类似的问题:
<a href="https://stackoverflow.com/questions/25964076/ios-8-top-layout-guide-auto-layout-problems" rel="noreferrer noopener nofollow">iOS 8 top layout guide auto layout problems</a>
但是没有给出可以普遍解决问题的答案。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果我将所有其他 View 作为 subview 添加到我的主视图中,框架的大小会正确(感谢自动布局),但这对我来说不是一个选项,因为我不能在启动时将它们全部作为 subview 。</p>

<p>这就是我让它为我工作的方式:</p>

<p>一旦我添加了帧错误的 subview ,我就会像这样向它添加自动布局约束:</p>

<pre><code>;//naming just for example

_subviewFromSameXib.translatesAutoresizingMaskIntoConstraints = NO;


[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_subviewFromSameXib
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeTop
                                                         multiplier:1.0
                                                         constant:0.0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_subviewFromSameXib
                                                          attribute:NSLayoutAttributeLeading
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeLeading
                                                         multiplier:1.0
                                                         constant:0.0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_subviewFromSameXib
                                                          attribute:NSLayoutAttributeBottom
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeBottom
                                                         multiplier:1.0
                                                         constant:0.0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_subviewFromSameXib
                                                          attribute:NSLayoutAttributeTrailing
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeTrailing
                                                         multiplier:1.0
                                                         constant:0.0]];
</code></pre>

<p>这将执行以下操作:
添加我的 subview 后,它会添加新的约束,告诉该 subview 它必须根据其父 View 调整大小。</p>

<p>这解决了我的问题,但没有解决主要问题:xib 中的多个 View 不会根据屏幕边界调整大小。只有第一个可以。</p>

<p>顺便说一句,我使用的是 xibs 而不是 Storyboard。我应该添加该 Playground 在所有设备上正确显示所有 View 。只有在运行时他们才会搞砸。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - iphone 6 plus 上具有 600x600 pt 框架的 UIViews 以及大小类,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/27298142/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/27298142/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - iphone 6 plus 上具有 600x600 pt 框架的 UIViews 以及大小类