Swift: Bring View from Stack View to Front

Swift: Bring View from Stack View to front

From the UIStackView documentation:

The order of [the views in] the subviews array defines the Z-order of the subviews. If the views overlap, subviews with a lower index appear behind subviews with a higher index.

So since all views in the stack view's arrangedSubviews array are also in the stack view's subviews array, you should be able to use the standard UIView APIs to rearrange the views z-order:

  • bringSubview(toFront:)
  • sendSubview(toBack:)
  • exchangeSubview(at:withSubviewAt:)

I don't know whether this is possible to do in Interface Builder or not.

Bring view to front in Swift

You can only modify the zPosition in the concept of a CALayer. UIView does not expose this functionality. It does however expose two methods; sendSubviewToBack: and bringSubviewToFront:. This would need to be called on the superview of the view you want to move, and would take the view you want to move as an argument.

Also note that if you simply set userInteractionEnabled = false on the top view, it will pass touches down to the view below it. This saves messing around with the view's positions.

How to bring a subview to front when selected in Swift?

Use this:

baseView.bringSubview(toFront: imageViewOne)

Bringing UIViews to front on tap

It won't work like in the link.

1, You need to setup the tap gesture recogniser

2, You need to create an @objc function and call this on action.

//paste it into viewDidLoad. 
let tap = UITapGestureRecognizer(target: self, action: #selector(tapped))
tap.cancelsTouchesInView = false
//Uncomment next line, if you want more than one tap to activate gesture.
//tap.numberOfTapsRequired = 5
self.view.addGestureRecognizer(tap)

// Create a function
@objc func tapped(){
self.view.bringSubviewToFront(UIViewWhatYouWantToBringToFront)
}

How to bring overlay view to front in swiftUI?

you need to apply your .zIndex(1) modifier on HStack containing your DropdownSelector like this

HStack(){
DropdownSelector(
placeholder: "Day of the week",
options: options,
onOptionSelected: { option in
print(option)
})
.padding(.horizontal)

DropdownSelector(
placeholder: "Day of the week",
options: options,
onOptionSelected: { option in
print(option)
})
.padding(.horizontal)
}
.zIndex(1)


Related Topics



Leave a reply



Submit