Normally developers try to bring subview to front. On contrary, how does one bring superview to front?
I'm looking for a reverse of bringSubviewToFront(_:). It should look like bringSuperviewToFront(_:)
I'm looking for a reverse of bringSubviewToFront(:). It should look like bringSuperviewToFront(:)
That doesn't exist. Instead of giving your button a subview, make both the subview and the button subviews of some container. Then you can adjust their relative z positions to suit your purpose.
I do not think you can do that. Views are laid down in layers with each sub view layer being part of super view layer. The effect you want to materialise can be achieved by hiding/removing all the sub views of the view you want to get Bring To Front effect on.
This works well with sub views because normally you would want to show/hide subviews inside same parent view based on their layer position!
In the spirit of being less rude.
func bringSubviewToFront(view: UIView) {
let superview = view.superview
superview?.addSubview(view)
}
That will bring it to the front. As in:
bringSubviewToFront(mySubview)
What the above code does is detach the view from wherever it is in its superview's hierarchy and reattach it to the top of all the subviews. This has the effect of bringing it to the front like you want. If the view isn't part of a hierarchy, then the function does nothing.
Related
My SCNView is full screen, but I would like to add a view in the bottom right corner like a minimap of a game. I have the code working separately in each of the views, but I have no idea how to view both of them at the same time.
If possible, I would also like it so that if the smaller view is touched, the position of the two views are swapped (the main view becomes the mini-view, and the mini-view becomes the main view) -- which could be repeated as needed to swap them out.
SCNView is a subclass of UIView so you can treat it like a regular view. There are too many different ways to handle this to cover them all. But for example, use addSubview to add one to the other. You could for example also use a popover view in the first scene, and add the minimap sceneview to the popover.
To swap them around you can use one master view (like the one of your main view controller) and then add the first sceneview, and to that sceneview add the mini map. When you tap the mini map you can simply remove them from their parents and add them again in the desired hierarchy. Another option would be to add both sceneviews to a master view and resize them and then use sendSubviewToBack on the sceneview that was the minimap.
I am new to iOS development. Currently I'm developing a part from my previous Android app for learning purpose. I want to do following animation. I don't know a proper name for the animation so check below gif.
I have both text label with text field inside stack view. Can some one share the code for this animation or post a helpful link?
For such a case, I would suggest to use a UIStackView (vertical) to be the container for the components in the scene. When working with stack views, you could easily get such an animation for free! For applying the desired animation, the simplest way is to implement the popular UIView animate(withDuration:animations:) and hide the desired view in its animations block parameter:
UIView.animate(withDuration: 1.0) {
self.viewToHide.isHidden = !self.viewToHide.isHidden
}
By following this approach, you would be able to create an animation -which is what are you looking for- like this:
Resource: Easy Animation with UIStackView.
Official Reference:
You could review Mysteries of Auto Layout, Part 1 Apple Session (at 00:12:22, it should contains the topic that you are asking about).
There are various ways you could handle that. Below is one way. (Note that Ahmad's suggestion of a stack view would be easier and cleaner.)
Place your views that you want to appear inside another view. I'll call this view a container view (although in this case I don't mean container view the way interface builder refers to a view that can hold a child view controller.) Set the clipsToBounds flag on the container view to true.
Add a height constraint to the container view.
Select the constraint and control drag into your source file to make an outlet to the constraint. Note the current height value of the height constraint.
Now change the height value to zero. This should cause the views inside the container view to disappear.
When you want to animate those controls into place use a UIView animation that contains a call to self.view.layoutIfNeeded(). Set the height constraint of the ContainerView to its nonzero value immediately before the call to UIView.animate(duration:animations:).
So I want to add a PanGestureRecognizer to a UIView class that I have made. Everything is working well, however: when I drag the view, it's subviews are also moving/updating etc.
-I am using SnapKit for my view's constraints
-I tried setting translatesAutoresizingMask to false, no result
-I tried to set constant constraints on the subviews,not result
Here is a gif of what it looks like:
https://media.giphy.com/media/oI0Wf5T6B0jIY/giphy.gif
Thanks for the help everyone.
This is expected and is how UIKit (and essentially all 2D/3D scene graphs) are designed to work. The geometry of a view is relative to its parent view. If the parent view moves, so do its decedents.
If you do not want a view to be dependent on the position of the view being moved, it probably should not be a subview of that view in the first place. It's possible to apply the inverse movement to the subviews to make it appear that they're not moving, but that is most likely a lot more work than just not making them subviews in the first place.
In my iOS app I want my users to be able to zoom in on the screen. My main uiview contains several subviews which contain images. I want my uipinchgesturerecognizer to either change the scale, or ideally use some "zoom" rather than scaling each subview.
Please and thank you.
This can be accomplished with UIScrollView. First create a scroll view as the base of your view hierarchy, putting your previous container view as a subview of the scroll view. Set the delegate of the scroll view to self and implement the delegate method viewForZoomingInScrollView, in which you should return the view that will be zoomed in (your original container view). This will allow the user to pinch and zoom your original UIView.
It's hard to provide advice on this without having a clearer view of what exactly you want to achieve.
Can you include a link to a sketch? For example, do you want the individual subviews to remain the same size but the layout to change ? Do you want the individual subviews to resize but their contents to be upscaled?
If you simple want to treat the subview as (basically) a single image which just happens to have other images in it, then maybe it would be better to render it as one and then scale that?
I created a container view that holds a bunch of child views - a collection view, a custom toolbar and some bits and pieces.
The design has a border on the top, left and right sides, but not the bottom, so I overrode drawRect to include border.
When I added the toolbar I noticed that it appears over the top of the border. (For some reason I initially thought it wouldn't but of course it does!).
Is there anyway I can tell drawRect to draw over the top of my subviews?
Of course there's loads of other ways to solve my problem (adjust the toolbar's frame for example) however I'm asking this question in order to get a deep understanding of how drawing works in relation to compositing and the view hierarchy.
Drawing happens beneath all subviews of a UIView. Think of it as being on the very base - an actual part of your view - and then each subview is added on top of your view. To make the drawing above the subviews is the same as wanting for the subviews to appear under the view, while still being subviews. Perhaps that analogy makes it clearer why it must always be on the bottom. And it also leads you logically to the solution:
To get the drawing to appear above subviews, simply create a new UIView subclass to place the drawing code inside, and place this above all other subviews.
It might also be a good idea to override your UIView's addSubview: implementation, to ensure your subview always remains on top.
I believe you can't, since the drawRect is called first for the view and when it has finished drawing drawRect is called for subviews to draw over it. Maybe just make another subview on top of that view that has the borders you need and is transparent everywhere else?
Subviews are drawn on top of their super views. So the answer to your question is no.
At the time when you draw the border on your container view, Cocoa hasn't even started drawing the toolbar yet.
I guess you could make the top of the border a subview or move the toolbar down a bit.