Objective C:方向变化的自适应工具栏

发布于 2024-12-16 13:23:15 字数 279 浏览 1 评论 0原文

当我更改 iPad 的方向时,我的工具栏出现问题。

我将笔尖文件设置为横向,一切都很好,但是当我将其转换为纵向时,我的工具栏仍然具有横向的宽度。

如何使我的工具栏适应方向更改为纵向?

景观: 在此处输入图像描述

肖像:

在此处输入图像描述

谢谢!

I am having a problem with my toolbar when i change the orientation of my iPad.

i set my nib file into landscape and everything is all right but when i turned it to portrait my toolbar still has the width from the landscape orientation.

how will i make my toolbar adaptive to the orientation change to portrait?

Landscape:
enter image description here

Portrait:

enter image description here

thanks!

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

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

发布评论

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

评论(2

疏忽 2024-12-23 13:23:15

尝试将 UIViewAutoresizingFlexibleWidth 添加到工具栏 autoresizingMask 中,如下所示:

myToolbar.autoresizingMask |= UIViewAutoresizingFlexibleWidth

或者,如果您在 Interface Builder 中执行此操作,请确保选择此水平栏(也可以选择其他水平栏,这很好):

在此处输入图像描述

更多内容来自 UIView 类参考< /a> 关于 autoresizingMask

当视图的边界发生变化时,该视图会自动调整其大小
根据每个子视图的自动调整大小掩码的子视图。您指定
通过组合中描述的常量来获得此掩码的值
UIViewAutoresizing 使用 C 按位​​ OR 运算符。结合这些
常量允许您指定视图的尺寸应该增长或
相对于超级视图缩小。该属性的默认值
UIViewAutoresizingNone,这表明视图不应该
完全调整了大小。

当沿同一轴设置多个选项时,默认
行为是按比例分配尺寸差异
灵活的部分。相对于柔性部分而言,柔性部分越大
其他灵活的部分,它就越有可能增长。例如,
假设此属性包括 UIViewAutoresizingFlexibleWidth 和
UIViewAutoresizingFlexibleRightMargin 常量,但不包括
UIViewAutoresizingFlexibleLeftMargin 常量,从而表明
视图左边距的宽度是固定的,但视图的
宽度和右边距可能会改变。因此,该视图似乎锚定于
其父视图的左侧,同时视图宽度和间隙
视图右侧增加。

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

Try adding UIViewAutoresizingFlexibleWidth to the toolbar autoresizingMask like so:

myToolbar.autoresizingMask |= UIViewAutoresizingFlexibleWidth

Or, if your doing this in the Interface Builder, make sure this horizontal bar is selected (others may be selected as well, which is fine):

enter image description here

More from the UIView Class Reference about autoresizingMask:

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.

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.

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.

与风相奔跑 2024-12-23 13:23:15

除了调整工具栏的灵活宽度之外,您还可以创建 2 个工具栏项目数组。一张用于肖像,一张用于风景。幸运的是,您只需创建工具栏项一次,然后将它们添加到适当的数组中即可。

然后,在方向更改期间,您可以将工具栏的项目数组设置为适当的数组。

祝你好运

In addition to adjusting the flexible width of your toolbar you could create 2 arrays of toolbar items. One for portrait and one for landscape. Fortunately you only have to create the toolbar items once and just add them to the appropriate array(s).

Then during the orientation change you can set the toolbar's items array to the appropriate one.

Good Luck

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