I want to move one view on top of another, how can I know the z index of the view, and how to move on to top?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Set the z-index value of a jQuery autocomplete inp
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
UIView
siblings are stacked in the order in which they are added to their superview. TheUIView
hierarchy methods and properties are there to manage view order. In UIView.h:The sibling views are ordered back to front in the
subviews
array. So the topmost view will be:and bottom view will be:
Like Kolin Krewinkel said,
[parentView bringSubviewToFront:view]
will bring the view to the top, but this is only the case if the views are all siblings in the hierarchy.If you are using cocos2d, you may see an issue with [parentView bringSubviewToFront:view], at least it was not working for me. Instead of bringing the view I wanted to the front, I send the other views back and that did the trick.
Within the view you want to bring to the top... (in swift)
IB and Swift
Given the flowing layout where yellow is the superview and red, green, and blue are sibling subviews of yellow,
the goal is to move a subview (let's say green) to the top.
In Interface Builder
In the Interface Builder all you need to do is drag the view you want showing on the top to the bottom of the list in the Documents Outline.
Alternatively, you can select the view and then in the menu go to Editor > Arrange > Send to Front.
In Swift
There are a couple of different ways to do this programmatically.
Method 1
An array of the subviews is contained in
yellowView.subviews
. Here,bringSubviewToFront
moves thegreenView
from index0
to2
. This can be observed withMethod 2
0
for all the other views, the result is that thegreenView
looks like it is on top. However, it still remains at index0
of theyellowView.subviews
array. This can cause some unexpected results, though, because things like tap events will still go first to the view with the highest index number. For that reason, it might be better to go with Method 1 above.zPosition
could be set toCGFloat.greatestFiniteMagnitude
(CGFloat(FLT_MAX)
in older versions of Swift) to ensure that it is on top.If you want to do this through XCode's Interface Builder, you can use the menu options under Editor->Arrangement. There you'll find "Send to Front", "Send to Back", etc.