xcode iPhone 界面生成器:Z 索引、按钮的 Z 顺序、图像、ui 元素等?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2276116/
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-09 03:17:28  来源:igfitidea点击:

IPhone Interface Builder: Z-Index, Z-order of a button, image, ui element, etc?

iphonexcodeinterface-builder

提问by foreyez

I'm dragging things around in the Interface Builder... I'd like to specify whether an image is in front (like an indicator) or behind of a button (like a background). I don't see any z-index property as I'm used to seeing on other environments. If there isn't a z-index property what is the best way to go about what I'm trying to accomplish? Thanks!

我在 Interface Builder 中拖动东西...我想指定图像是在按钮前面(如指示器)还是在按钮后面(如背景)。我没有看到任何 z-index 属性,因为我习惯于在其他环境中看到。如果没有 z-index 属性,那么完成我要完成的任务的最佳方法是什么?谢谢!

回答by foreyez

I achieved what I wanted by clicking on a ui element (button, image, text, etc) and going to the Layout menu (at the top of screen) and then I used "bring to front", "send to back", etc.

我通过单击 ui 元素(按钮、图像、文本等)并转到“布局”菜单(在屏幕顶部)来实现我想要的效果,然后我使用“置于前面”、“置于后面”等.

In Xcode 4.2 you'll find the options in menu up top: Editor->Arrange

在 Xcode 4.2 中,您将在顶部菜单中找到选项:Editor->Arrange

回答by Kendall Helmstetter Gelner

There is a Z ordering without using subviews. For one thing there are menu options for "send to front" and "send to back". Also however, if you look at the elements in your view as a tree of elements, you can re-order them there (rather than in the view itself) just by dragging.

在不使用子视图的情况下有一个 Z 排序。一方面,有“发送到前面”和“发送到后面”的菜单选项。但是,如果您将视图中的元素视为元素树,则只需拖动即可在其中(而不是在视图本身中)对它们重新排序。

Subviews are great for grouping but not as useful for ordering (except that the whole "set" stays at the same level).

子视图非常适合分组,但对排序不太有用(除了整个“集合”保持在同一级别)。

回答by e.James

In XCode development, a UI element, or "view" is in front of another view when it is a subview of that view. For example, if view B is a background and view C is a control, to place the control above the background (i.e. closer to the user), you would make view C a subview of view B. In Interface Builder, this is accomplished by dragging the control intothe background.

在 XCode 开发中,当 UI 元素或“视图”是该视图的子视图时,它位于另一个视图的前面。例如,如果视图 B 是一个背景而视图 C 是一个控件,为了将控件放置在背景之上(即更靠近用户),您可以使视图 C 成为视图 B 的子视图。在界面生成器中,这是通过将控件拖到后台。

Essentially, you are looking at a tree structure, with the views in the background being near the root of the tree, and views in the foreground (closer to the user) being near the leaves of the tree.

本质上,您正在查看树结构,背景中的视图靠近树的根部,而前景中的视图(靠近用户)靠近树的叶子。

The Windows and Viewsdocument from Apple's iPhone developer documentation may help to clear things up.

Apple 的 iPhone 开发人员文档中的Windows 和 Views文档可能有助于解决问题。

Note 1:You should almost never overlap individual controls, such as buttons and text fields. Doing so goes against Apple's user interface guidelines. You can, of course, still do this if you want to, but you need to be aware that you are stepping out of the safety zone. If you are simply writing a "normal" iPhone application, your best bet is to stick to Apple's way of doing things.

注意 1:您几乎不应该重叠单个控件,例如按钮和文本字段。这样做违反了 Apple 的用户界面指南。你可以,当然,还是做这个,如果你想,但你需要知道你是在安全区走出。如果您只是在编写一个“普通”的 iPhone 应用程序,那么最好的办法就是坚持 Apple 的做事方式。

Note 2:If, for some reason, you do need things to overlap in a specific way, you can make use of CALayerobjects to keep everything properly ordered. CALayer objects are part of Apple's Core Animation technology.

注意 2:如果由于某种原因,您确实需要以特定方式重叠,您可以使用CALayer对象来保持所有内容的正确排序。CALayer 对象是 Apple 的核心动画技术的一部分。

回答by Eric Colton

In XCode 4.6 Interface builder, bring up the "Document Outline" by the menu (Editor->Show Document Outline) or clicking the translucent button in the lower left corner of the canvas. Under the "Objects" listing, the Z-ordering of the views is displayed and the user can drag them around to change the ordering amongst each other. The closer to the bottom of the screen, the "closer" that element is to the user.

在XCode 4.6 Interface builder中,通过菜单(Editor->Show Document Outline)或点击画布左下角的半透明按钮调出“文档大纲”。在“对象”列表下,显示了视图的 Z 顺序,用户可以拖动它们以更改彼此之间的顺序。离屏幕底部越近,该元素离用户越“近”。

回答by Kevin

I know it's an old question, but in xcode 4.6 I can just drag elements up and down in the list to the left. The lower the element in the list, the closer it is to the user e.g. the bottom element in the list will always be visible, unless it's off the screen. I'm not sure when this was implemented, but it seems easier than the other answers.

我知道这是一个老问题,但是在 xcode 4.6 中,我可以将列表中的元素向上和向下拖动到左侧。列表中的元素越低,它离用户越近,例如列表中的底部元素将始终可见,除非它不在屏幕上。我不确定这是什么时候实施的,但它似乎比其他答案更容易。

回答by Mr. Tann

In Swift:

在斯威夫特:

Within the view you want to bring to the top

在您想要带到顶部的视图中

     superview?.bringSubviewToFront(self)

回答by user4401104

What worked for me was doing it in code:

对我有用的是用代码做的:

self.view.sendSubviewToBack(imageView);

I don't know why but none of the other answers fixed it for good. I put the line above in the viewDidLoad method.

我不知道为什么,但其他答案都没有永久修复它。我将上面的行放在 viewDidLoad 方法中。