如果由用于 Xcode 4.5 和 iPhone 5 的 Interface Builder 构建,如何使视图中的控件不重叠?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12549106/
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
How to make controls in view not overlap if built by Interface Builder for Xcode 4.5 and iPhone 5?
提问by Jeremy L
I am using Xcode 4.5 and iPhone 4 and 5 simulator, and the Interface Builder would add a UIButton in the top half of the screen with a top constraint, and add a UIButton in the bottom half of the screen with a bottom constraint.
我正在使用 Xcode 4.5 和 iPhone 4 和 5 模拟器,Interface Builder 会在屏幕的上半部分添加一个带有顶部约束的 UIButton,并在屏幕的下半部分添加一个带有底部约束的 UIButton。
It works fine on an iPhone 5 simulator, but on the iPhone 4 simulator, the buttons can overlap, or the bottom button may even get positioned above the top button.
它在 iPhone 5 模拟器上运行良好,但在 iPhone 4 模拟器上,按钮可以重叠,或者底部按钮甚至可能位于顶部按钮上方。
I think it is due to the constraint, such as the bottom button "must be 250 points away from the bottom margin". I can't delete the constraint, and if I change it to "250 points or less", it won't work, if I change it to "250 points or more", it won't work either.
我认为这是由于约束,例如底部按钮“必须离底部边距250点”。我无法删除约束,如果我将其更改为“250 点或更少”,它将不起作用,如果我将其更改为“250 点或更多”,它也将不起作用。
Is there a way to:
有没有办法:
1) Make it have no constraint, but just position at absolute x and y? (or what about the spring in the past, so that everything is more spaced out in iPhone 5?)
1)让它没有约束,只是在绝对x和y的位置?(或者过去的春天怎么样,让 iPhone 5 中的一切都更加间隔?)
2) Make the NIB into a one for iPhone 4 and 4S only, so that the app works well on iPhone 4 and 4S and just "black barred" on the iPhone 5.
2) 将 NIB 变成仅适用于 iPhone 4 和 4S 的笔尖,以便该应用程序在 iPhone 4 和 4S 上运行良好,而在 iPhone 5 上只是“黑条”。
3) Make it work well on iPhone 5 and work well on iPhone 4 as well?
3) 让它在 iPhone 5 上运行良好,在 iPhone 4 上运行良好?
If you know solutions to only (1), (2), or (3), above, please give it regardless, as it is still a viable solution for the transition period.
如果您只知道上述 (1)、(2) 或 (3) 的解决方案,请尽管提供,因为它仍然是过渡期的可行解决方案。
It is actually very easy to reproduce: Create a simple Single View app using Xcode 4.5, and drag one button just above the center point of the screen, and another one just below the center point of the screen. Then run it on the Simulator. On iPhone 5, it is:
它实际上很容易重现:使用 Xcode 4.5 创建一个简单的 Single View 应用程序,然后将一个按钮拖到屏幕中心点上方,另一个按钮在屏幕中心点下方。然后在模拟器上运行。在 iPhone 5 上,它是:
And now stop the app, and change the Device in the Simulator to iPhone 4S (3.5 inch Retina). If you don't stop the app first, the Simulator can crash. Now run the app again, and the buttons will overlap:
现在停止应用程序,并将模拟器中的设备更改为 iPhone 4S(3.5 英寸 Retina)。如果您不先停止应用程序,模拟器可能会崩溃。现在再次运行应用程序,按钮将重叠:
回答by Jay Haase
I had the same problem, and it was due to a wrong settings for autosizing. To change the autosizing, click on the control in the interface builder, go to the size inspector.
我遇到了同样的问题,这是由于自动调整大小设置错误造成的。要更改自动调整大小,请单击界面构建器中的控件,转到大小检查器。
Notice the bold red I's in the autosizing grid, they control placement when the screen is resized (like when going from iPhone 3GS to iPhone 5.
请注意自动调整网格中的粗体红色 I,它们在调整屏幕大小时控制位置(例如从 iPhone 3GS 到 iPhone 5。
I think it will work best for you if you make your autosizing look like this:
我认为如果您使自动调整大小看起来像这样,它将最适合您:
回答by tc.
- I'm not sure, since I haven't used constraint-based layout (and won't until we drop support for iOS 5). Traditional NIB-based autoresizing will do absolute coordinates.
- You can't do that for just one screen, as far as I know — you can disable it for your whole app by removing
[email protected]
. - I'd go for programmatic layout, or if you want the easy way (which will be a pain to maintain), different nibs for different screen sizes. Note that you'll want to correctly handle the in-call status bar too...
- 我不确定,因为我还没有使用基于约束的布局(并且在我们放弃对 iOS 5 的支持之前不会使用)。传统的基于 NIB 的自动调整大小将执行绝对坐标。
- 据我所知,你不能只在一个屏幕上这样做——你可以通过删除
[email protected]
. - 我会去编程布局,或者如果你想要简单的方法(这将是一个痛苦的维护),不同屏幕尺寸的不同笔尖。请注意,您也需要正确处理通话状态栏...
EDIT:Put the two buttons in a container view and make the constraint center the container inside its parent? It won't adjust the spacing between buttons, but should look okay on both devices.
编辑:将两个按钮放在容器视图中并使约束中心成为其父级内的容器?它不会调整按钮之间的间距,但在两个设备上看起来都不错。
回答by pronvit
Select your buttons and add "vertical spacing" constraint for them. And set "less than" for top/bottom margins.
选择您的按钮并为它们添加“垂直间距”约束。并为顶部/底部边距设置“小于”。