xcode 为界面生成器中的视图更改两个方向的布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8312466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:35:48  来源:igfitidea点击:

changing layout for two orientations for a view from interface builder

iphoneobjective-ciosxcode

提问by JAHelia

is it possible to set the layout for a view for landscape and portrait orientations from interface builder (XCode 4.1)? I'm asking this question because when I set the layout for portrait, and then change view orientation to landscape and re-set the layout again according to landscape, it will keep with the last layout for landscape orientation !! that means, when I switch back to portrait orientation the layout does not change.

是否可以从界面构建器(XCode 4.1)设置横向和纵向视图的布局?我问这个问题是因为当我设置纵向布局,然后将视图方向更改为横向并根据横向再次重新设置布局时,它将保持横向布局的最后一个布局!这意味着,当我切换回纵向时,布局不会改变。

any solution to this problem from IB ?

IB 有解决这个问题的方法吗?

EDIT:

编辑:

I understood the idea now, so IB will give you limited flexibility for playing with my view in both orientations, it's limited because I can't produce a fully new layout for my view for each orientation from IB, for example, If I have six UIButtons(with their shadow image views) placed on a portrait orientation as in the following screenshot: enter image description here

我现在理解了这个想法,所以 IB 会给你有限的灵活性来在两个方向上使用我的视图,这是有限的,因为我无法为 IB 的每个方向为我的视图生成一个全新的布局,例如,如果我有六个UIButtons(带有它们的阴影图像视图)以纵向放置,如下面的屏幕截图所示:在此处输入图片说明

and giving each button and the shadow image view all resizing masks as in this screenshot:

并为每个按钮和阴影图像查看所有调整大小的蒙版,如下图所示:

enter image description here

在此处输入图片说明

the result in landscape will be messed up as in this screenshot:

横向的结果会像这个截图一样混乱:

enter image description here

在此处输入图片说明

so I have two questions:

所以我有两个问题:

1) is it possible to set auto-resizing for these objects (from IB) so that the landscape view will match the portrait one ? if yes how ?

1) 是否可以为这些对象(来自 IB)设置自动调整大小,以便横向视图与纵向视图相匹配?如果是怎么办?

2) can I design (from IB) a completely different layout for landscape (without affecting the original layout in portrait) as in the following screenshot:

2)我可以(从IB)设计一个完全不同的横向布局(不影响纵向的原始布局),如下面的截图所示:

enter image description here

在此处输入图片说明

If i can't, I think the solution would be to make a new view from code, is that true ?

如果我不能,我认为解决方案是从代码中创建一个新视图,这是真的吗?

thank you for your help.

感谢您的帮助。

回答by Niko

To automatically change the layout of your view in interface builder, you have to set the autoresizing mask option of all graphical objects of your view. You can either do it programmatically like :

要在界面构建器中自动更改视图的布局,您必须设置视图的所有图形对象的自动调整大小掩码选项。您可以像以下那样以编程方式执行此操作:

myObj.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

Or with Interface builder in the Size Inspector tab of each object. You should see something like this :

或者使用每个对象的“大小检查器”选项卡中的“界面”构建器。你应该看到这样的:

enter image description here

在此处输入图片说明

When the line is red, it means the margin (regarding the object container) will stay the same if device orientation change. In other case, the margin will change to adapt the object placement in the view container.

当线条为红色时,这意味着如果设备方向改变,边距(关于对象容器)将保持不变。在其他情况下,边距将更改以适应视图容器中的对象放置。

Unlikely, when a row is in NOT in red, it means the size of teh object will stay the same in all device orientations, else the size will be modified (useful for background image for example)

不太可能,当一行不是红色时,这意味着对象的大小将在所有设备方向上保持不变,否则大小将被修改(例如对背景图像有用)

So, in the example above the object size will not change, and the left and top margin will not change either regarding its container.

所以,在上面的例子中,对象大小不会改变,左边和上边距也不会改变它的容器。

Hope this is clear enough. Do not hesitate to react in case it's not clear

希望这已经足够清楚了。如果不清楚,请毫不犹豫地做出反应

EDIT

编辑

You can check the result of your resizing mask without re-compiling the application simply by doing the trick below :

您只需执行以下技巧即可检查调整大小掩码的结果,而无需重新编译应用程序:

  • Select the uiviewcontroller main view (self.view)
  • Go to the Attributes Inspector
  • In section "Simulated Metrics" you should see an 'Orientation' Item
  • Switch off and on from 'Portrait' to 'Landscape' to see the resulted view
  • 选择uiviewcontroller主视图(self.view)
  • 转到属性检查器
  • 在“模拟指标”部分,您应该看到“方向”项
  • 从“人像”到“风景”关闭再打开以查看结果视图

EDIT1

编辑1

If I correctly understand what you means, I think you can do it this way :

如果我正确理解你的意思,我认为你可以这样做:

enter image description here

在此处输入图片说明

Placing your button and your shadow image view in a view container. Then set the view container autoresizing as you are doing right know for your button and lock the button and image view size/margins regarding this view container.

将按钮和阴影图像视图放在视图容器中。然后设置视图容器自动调整大小,正如您对按钮所做的那样,并锁定有关此视图容器的按钮和图像视图大小/边距。

That should work.

那应该有效。

回答by pronvit

It's not a problem of IB of course. There's just no built-in mechanism for switching to completely different layout on orientation change.

这当然不是IB的问题。只是没有内置机制可以在方向更改时切换到完全不同的布局。

If your objects (button/shadow) are of different sizes then you have to group them into views as @Niko said for autoresizing masks to work correctly.

如果您的对象(按钮/阴影)具有不同的大小,那么您必须按照@Niko 所说的将它们分组到视图中,以便自动调整蒙版大小才能正常工作。