Hide non child UIView in UIView - ios

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.

Related

Why is my UIView drawing behind my other views?

I have a UIView which is opened and closed by a button onscreen. I am positioning it over a scrollable custom text area which is a bunch of UILabels added to a UIScrollView.
I've made sure that it is added last in my view hierarchy, in the xib. I have also tried calling bringSubviewToFront, and have even removed the UIView from the parent view and re-added it after doing the layout.
Nothing that I have tried prevents it from being drawn behind this UIScrollView. I have verified that my UIView is actually the last UIView in the hierarchy by looking at the view hierarchy in the debugger at runtime.
The UIView in question, shown as a smiley in the picture here, has children. It has a 3x3 grid of invisible UIButtons, and a UIImageView to draw the image, which is normally something other than the smiley. But I am operating on the parent UIView, not the children.
Is there anything I can try, to help diagnose this? To recap, I have done all the following:
the UIView is added last in my view hierarchy, after the UIScrollView. It is a child of the same parent (the root view) as the scroll view - hence, a sibling view of the scroll view.
after doing the layout, I've tried [self.view bringSubviewToFront:myView]
after doing the layout, I've tried doing [myView removeFromSuperView] followed by [self.view addSubview:myView].
The latter two operations are performed at the very end of my layout method.
Any ideas?

Embedding a view with subviews in another parent view and keeping constraints (IB)

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.

Is it good practice to make a view subview of the main view, not a view it visually lies upon?

Say in my view controller I have a custom UIView which holds certain area inside view controller's view. Let's call this view viewA. And I have a custom UIView called viewB which lies within the bounds of viewA. I used to think that viewB MUST be a subview of viewA simply because it lies within its bounds. But today I got into an argument with a colleague of mine, who said that viewB is not necessarily should be a subview of viewA but a subview of the view controller's view instead. What do you think? Is there a common rule regarding this issue?
I think there is no such thing that viewB MUST be a subview of viewA simply because it lies within its bounds.
The view hierarchy is organized by UIView's array property subViews. Each subview has their own frame information to layout relative to parent's bounds. Overlapping is normal
In my opinion it would depend on the usage of viewA and viewB. If you always want to position viewB relative inside viewA's bounds, or of if you always want to use viewA and viewB together with each other it would probably be simpler to add viewB as a subview. If you want to position and use these two views separately or if the positions of these two views are not related per se I would say that they should be separate views.
In short, just because views overlap does not mean they belong together / that one should be superview for the other.
UITableViewCells subviews of UITableView, not because they are with the bounds of the UITableView, but they have internal connections.
In your case, you need to think whether viewA and viewB have some real relationships or just happen to be together. Maybe viewA accesses and modifies viewB a lot? Or viewB is an component of viewA? That's when you need to set viewB to be viewA's subview.

Animating UIView with auto layout when the view is just created and about to appear

I have a UIView that contains subviews that contains subviews...(Hierarchy of UIViews).
Every UIView is set with auto layout. When I press a button, this UIView is created and then I would like it to drop from the top of the screen.
The problem when I use layoutIfNeeded in an animation is that if will animate everything at the same time as it is a newly created UIView (not yet display).
My question is, is there a way to do the animation of only the UIView dropping with all the subviews already laid out in it?
I guess you can do it by creating the UIView when UIViewController displays and then hide it but I was wondering it there was another way.
Cheers
If you call layoutIfNeeded right after you add it to a view, while still outside the view, it should lay out it's views correctly. And then in the animation you call it again so the view animate to the final position.

How to make UIButton overhang over parent view and still make it all selectable?

I have a UIButton which is a child of a custom UIView. I've placed the button so its center is directly on top of the top right pixel of its parent view. Currently only 25% of the button (the area of the button which is within the the parent's view bound) is selectable. Is there a way of making the rest of the 75% selectable?
Here's a link to a diagram of the problem
Thanks!
Since your button is a subview of the bigger parent view, touch events are not registered outside the bounds of the parent view.
I recommend that you add the button as a subview to the same view that the bigger view is in. You will have to convert the frame location to keep it positioned the same way.
UPDATE
You can convert coordinates automatically using the UIView convertRect:toView: method.

Resources