When a UIView does not have a superview when loaded in iOS app? - ios

From developer reference:
(void)sizeToFit
Call this method when you want to resize the current view so that it
uses the most appropriate amount of space. Specific UIKit views resize
themselves according to their own internal needs. In some cases, if a
view does not have a superview, it may size itself to the screen
bounds. Thus, if you want a given view to size itself to its parent
view, you should add it to the parent view before calling this method.
Is there a case when a view does not have a superview? What are those cases?

a view doesnt have a superview when the view is the rootview meaing that there's no other view behind it (besides the window possibly)
OR when it was never added to a view obviously :D

If it is your View Controller's view, it doesn't have a superview.

Related

Hide non child UIView in UIView

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.

Is it possible to move UIView outside a controller without being cut?

I have 2 UIViewControllers. One is the content an the other is a Slide menu.
And I want to move the button from Controller 2 outside its superview like this
Is that possible? Because I tried to set clipToBounds = NO for the superview. The only result I get is this:
Any suggestion?
Several options here. The most obvious is clipToBounds = NO on the Controller 2's view, and any further subview which the button is contained in.
If you do not wish to set it to NO, then the view cannot be part of the clipped view hierarchy. You then have to move it to a shared superview, such as the container controller's view, the window, etc.

UICollectionVIew inside UIView adds a huge amount of padding to the top of the collectionView

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.

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.

Resources