UIView 自动调整资源大小

发布于 2024-08-18 14:58:20 字数 459 浏览 4 评论 0原文

我一直致力于自动调整自定义 UIView 的大小,而 Apple 对此几乎没有什么可说的。更重要的是,根据SO反馈,苹果的文档在某些方面实际上是错误的。最后,尽管 Interface Builder 中有六个调整大小组件,setAutoresizingMask: 有六个调整大小组件,但它们似乎根本不相关。例如,在旋转设备时,在 IB 中设置宽度弹簧与将 autoresizingMask 设置为 UIViewAutoresizingFlexibleWidth 具有不同的效果。

有没有什么好的资源可以帮助您了解 iPhone 上如何调整大小?

编辑:

我了解Apple使用的基本概念,例如contentMode,并且我已经阅读了UIView文档。我正在寻找一些东西,可以更深入地解释 IB 设置在 SDK 中以编程方式提供的功能方面的作用。

I've been working on autoresizing a custom UIView and Apple has very little to say on this. What's more, based on SO feedback, Apple's documentation is actually wrong in some areas. Finally, even though there are six resizing components in Interface Builder and six for setAutoresizingMask: they don't seem to correlate at all. For example, setting the width spring in IB has a different effect from setting the autoresizingMask to UIViewAutoresizingFlexibleWidth when rotating the device.

Are there any good resources for learning about how resizing works on the iPhone?

Edit:

I understand the basic concepts Apple uses such as contentMode, and I've read through the UIView documentation. I'm looking for something that explains a little more deeply what the IB settings do in terms of what's available in the SDK programmatically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你列表最软的妹 2024-08-25 14:58:20

autoresizingMask 设置为 UIViewAutoresizingFlexibleWidth,相当于设置宽度弹簧,加上 Interface Builder 中的左右支柱(支柱意味着边缘不灵活)。如果您想复制仅设置宽度弹簧而不设置左右支柱的行为,则必须将 autoresizingMask 设置为 UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleLeftMargin UIViewAutoresizingFlexibleRightMargin

setting the autoresizingMask to UIViewAutoresizingFlexibleWidth, is equivalent to setting the width spring, plus both the left and right struts in Interface Builder (the struts mean that the edge is not flexible). If you wanted to replicate the behavior of only setting the width spring, but not the left and right struts, you would have to set autoresizingMask to UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin

时光暖心i 2024-08-25 14:58:20

来自苹果:

...通过组合中描述的常量来指定此掩码的值
UIViewAutoresizing 使用 C 按位​​ OR 运算符。结合这些
常量允许您指定视图的尺寸应该增长或
相对于超级视图缩小。该属性的默认值
是 UIViewAutoresizingNone,这表明视图不应该
完全调整了大小。

...例如,
假设此属性包括 UIViewAutoresizingFlexibleWidth 和
UIViewAutoresizingFlexibleRightMargin 常量,但不包括
UIViewAutoresizingFlexibleLeftMargin 常量,因此表明
视图左边距的宽度是固定的,但是视图的
宽度和右边距可能会改变。因此,该视图似乎锚定于
其父视图的左侧,同时视图宽度和间隙
视图右侧增加。

如果自动调整大小行为没有提供您想要的精确布局
需要您的视图,您可以使用自定义容器视图并覆盖
它的layoutSubviews方法可以更精确地定位子视图。

因此,如果您想将视图定位到其超级视图的顶部和右侧,您可以应用如下蒙版:

[myView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin];

From Apple:

... 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.

... 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.

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.

So if you would like to position a view anchored to the top and right side of its superview, you might apply a mask like:

[myView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin];
懷念過去 2024-08-25 14:58:20

superview 具有 contentModeautoresizesSubviews 等参数,所有这些参数都带有其子视图的 autoresizingMask 使得调整大小行为能够被

仔细阅读 http://developer.apple.com/library/ios/#documentation/ uikit/reference/UIView_Class/UIView/UIView.html

superview has such parameters as contentMode and autoresizesSubviews, wich all with autoresizingMask of its subviews makes resizing behavior

read attentively http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html

我是男神闪亮亮 2024-08-25 14:58:20

根据记录,我挠头一小时,因为我的单行 UILabel 不会自动调整字体大小以适应宽度。最后,这成功了:

label.contentMode = UIViewContentModeScaleAspectFit;

我用谷歌搜索,似乎没有人澄清这个细节(每个人都告诉你标签应该是 1 行,设置 adjustmentFontSizeToFitWidth/minimumFontSize 等)。
也许这个属性的默认值改变了?我使用的是 iOS 4.3。

For the record, I scratched my head for an hour because my one-line UILabel just would NOT auto-adjust font size to fit width. Finally, this did the trick:

label.contentMode = UIViewContentModeScaleAspectFit;

I googled and no one seems to clarify this detail (everyone tells you the label should be 1 line, set adjustsFontSizeToFitWidth/minimumFontSize etc.).
Perhaps this property's default value changed? I'm on iOS 4.3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文