I got a Stack View with view: 1,2,3,4 and 5
Looking for a method to bring the Image View4 from view4 in the Stack view, above all the other views. The reason I need this is because I am moving the Image View4 over the Image View3 in view3.
I have tried to move view3 over view4 in the view hierarchy, but that just swaps their places in the Stack View.
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.
Remove the image from the stack view and add it to the same view that contains the stack view as a subview, using addSubview(_:). That will put it on top of all other views. You could also use insertSubview(_:aboveSubview:) to insert it directly above the stack view.
You'll need to add constraints to the new view so that it is positioned where you want it.
You can have IBOutlet property in UIViewController but in view hierarchy it is placed in UIStackView.
So you should use
stackView.bringSubviewToFront(myView)
rather then
self.view.bringSubviewToFront(myView)
Related
Every view has a layer that does the rendering. What confuses me is that you can't set the corner radius or border to the stack view, yet when you hide one of the arranged subviews the rest of them fill up the stack view.
Furthermore, Apple doc says that the stack view only manages its arranged subviews.
But, how does the stack view resize its subviews? Does the stack view's layer do the resizing when an arranged subview is hidden? If not, what is then the usage of the layer property in a stack view?
The UIStackView is a nonrendering subclass of UIView; that is, it does not provide any user interface of its own. Instead, it just manages the position and size of its arranged views. As a result, some properties (like backgroundColor) have no effect on the stack view. Similarly, you cannot override layerClass, draw(:), or draw(:in:).
consider placing the stack view inside another UIView and giving that view a corner radius or border.
Apple Doc
I am currently working to build my UI with the use of xcode/storyboard. I have a situation where my shadows are under my next stack view row. I tried to bring the UIView1 to front without any luck. There is no gap between the "rows". I see the shadows when I changed the height of UIView1 so I know I have a shadow.
StackView
---- Row with UIView1 + Shadow
---- Row with UIView2
---- Row etc..
I'm missing something but can't get it to work yet.
No need for a separate root view. Just take the shadow out of the stack view, put it above, then use Autolayout constraints to put it right below your UIView1 where it should be (shadow.top = view1.bottom or so to say).
Autolayout is not reserved to constraints between siblings or children, you can add rules between any view in your view hierarchy.
Stack view are only abstract container for views, Stack view does not get rendered on view, So If you are trying to add shadow on stack view then i suppose -
You have to place a view before stack view For this follow following steps
embed a root view to the stack view on which you are willing to add shadow.
Add shadow on that root view.
I want to hide a non child UIView in a UIView but I don't know how to do it.
To be specific, I have a UITableView. Each UITableViewCell has another view inside it (a wrapper view called wrapperView). The wrapper has some labels set up in IB and some created programmatically. I have created a custom slide mechanism that reveals buttons under the wrapper (like the standard one does).
The labels created programmatically don't exceed wrapper's bounds because it clips the subviews. The problem is with the labels created in IB. They are the subviews of contentView.
How can this be solved? Is there a way for a UIView to clip other views on the same level(not parents nor children)? Or "transfer" the labels to the wrapper view?
It isn't completely clear what you're asking. A view will only clip it's subviews, not views that happen to fall within their frame rectangle but aren't subviews.
If you want to move a view object from one view hierarchy to another you can use addSubview(_:) to do so. That will automatically remove it from it's current parent view. To quote the Apple docs:
Views can have only one superview. If view already has a superview and
that view is not the receiver, this method removes the previous
superview before making the receiver its new superview.
I am trying to add collection view to UIView and there is a problem that doesn't make any sense. Screenshot. I made the background color of the collection view, to make show the view area more clearly.
If I add the collection view straight to the controller (just like in UICollectionViewController), the top padding doesn't exist.
In this example, I have autolayouted the collection view to resize to the whole view but the problem existed the moment I added collection view to the UIView.
Also, if I add another collection view right after this view, there items start from the top, without any margins.
If there is anything else you need me to provide, I will do it. I think this is a storyboard bug or something because there are no insets, in the view's attributes.
I have a scene in storyboard with several subviews for which I already set a lot of constraints in Interface Builder. These subviews are directly children of the view controller's view. Now I realize that I need the whole view + subviews be contained in a full-screen UIScrollView to handle scrolling of the content when the keyboard is shown. But if I simply move all the subviews inside the scroll view, I loose the constraints that referred to "superview" and I get a mess.
Is there any way to migrate the constraints regarding the previous "superview" (view controller's view) to the new parent scroll view (which is the child of the "superview")?
Thanks
No there is no way to do this. The constraints between the view controller's view and the subviews are as you know broken when you move your subviews into another subview/view. AFIAK the biggest reason for this is you could end up with subviews that have constraints which relate to views on longer in their
view hierarchy which is a violate of the constraints lay out system.