菜鸟教程小白 发表于 2022-12-11 20:24:51

iphone - 是否有可能有一个 XIB 然后设置大小并让所有东西都缩放以适应 UIView?


                                            <p><p>我是 iOS 开发的新手,我不确定我想做的事情是否可行。
基本上我正在通过网络服务检索一些产品,我想在屏幕上显示每一个产品,每行 4 个方 block 。这些方 block 中的每一个(带有图像和产品名称)都是一个 UIView,它有一个 .h、.m 和 xib 文件。我这样创建每个 View :</p>

<pre><code>CategoryItemView *catItem = [[ loadNibNamed:@&#34;CategoryItemView&#34; owner:self options:nil] objectAtIndex:0];
;
</code></pre>

<p>我把它定位为:</p>

<pre><code>while (colCount &lt; cols &amp;&amp; items &lt; ) {
CGRectviewRect = CGRectMake( x * colCount + pad , (row * (x-pad)) + pad*row + pad, x - pad, x - pad );
CategoryItemView* myView = ;
myView.frame = viewRect;
;

colCount++;
items++;
}
</code></pre>

<p>我想使用 xib 来布局所有元素。但是我找不到布局 xib 的方法,这样当像这样定位 UIView 时,所有元素都被缩放以适应并保持它们在 UIView 中的相对位置。这可能吗?</p>

<p>更新:您可以在此处查看 2 类示例,但该示例不起作用 <a href="http://home.glasscubes.com/share/s/d57sb19" rel="noreferrer noopener nofollow">http://home.glasscubes.com/share/s/d57sb19</a> </p>

<p>谢谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>autoresizingMask 是您在这些 View 上寻找的属性。</p>

<p>来自文档:</p>

<blockquote>
<p>autoresizingMask</p>

<p>An integer bit mask that determines how the receiver resizes itself
when its superview’s bounds change.</p>

<p>@property(nonatomic) UIViewAutoresizing autoresizingMask</p>

<p>Discussion</p>

<p>When a view’s bounds change, that view automatically resizes its
subviews according to each subview’s autoresizing mask. You specify
the value of this mask by combining the constants described in
UIViewAutoresizing using the C bitwise OR operator. Combining these
constants lets you specify which dimensions of the view should grow or
shrink relative to the superview. The default value of this property
is UIViewAutoresizingNone, which indicates that the view should not be
resized at all.</p>

<p>When more than one option along the same axis is set, the default
behavior is to distribute the size difference proportionally among the
flexible portions. The larger the flexible portion, relative to the
other flexible portions, the more it is likely to grow. For example,
suppose this property includes the UIViewAutoresizingFlexibleWidth and
UIViewAutoresizingFlexibleRightMargin constants but does not include
the UIViewAutoresizingFlexibleLeftMargin constant, thus indicating
that the width of the view’s left margin is fixed but that the view’s
width and right margin may change. Thus, the view appears anchored to
the left side of its superview while both the view width and the gap
to the right of the view increase.</p>

<p>If the autoresizing behaviors do not offer the precise layout that you
need for your views, you can use a custom container view and override
its layoutSubviews method to position your subviews more precisely.</p>
</blockquote></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 是否有可能有一个 XIB 然后设置大小并让所有东西都缩放以适应 UIView?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/7924202/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/7924202/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 是否有可能有一个 XIB 然后设置大小并让所有东西都缩放以适应 UIView?