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

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.

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.

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.

UIScrollView not passing touches to subviews?

I have a scrollview, I embed 3 controllers inside this scrollView in storyboard, and page between them horizontally.
content size of the scrollview is (scrollViewWith * numberOfControllers , scrollViewHeight)
scrollview appears and I am able to scroll horizontally thought the views, but the views don't detect any touch. (The controllers embedded have collectionViews inside them)
I implemented touchesBegin in one of the controllers, and it's never getting called.
Any idea what could cause the scrollview not to pass touches to subviews?
This isn't necessarily an answer to why touch events aren't making it to the collection views, but it sounds like you're reimplementing UIPageViewController with the transitionStyle set to .Scroll. Maybe consider using that instead?
when adding views to a controller through embedded segue, the view of the viewController is not added directly inside of the main view, but it's embedded in another container view before being added to the main view
ViewControllerView->EmbedView->EmbededViewControllerView
So I ended up removing them both, and manually add the view of the viewControllers to the scrollView
what kinds of view's you have inside every page of the UIScrollView?
You can add anything there, for example a UICollectionView, and the only way to prevent to not pass touches inside is having the UIScrollView set as:
self.scrollView.userInteractionEnabled = NO;

nest a UIView into another UIView in storyboard without lossing contents layout

In Xcode 5, using storyboards:
Take a UIView , viewA
Make it the full size of the screen
Fill it with buttons and images
Make a second UIView, viewB
Take viewA and nest it inside viewB (drag and drop)
It will nest, but all of the objects in viewA get moved to a pile in the center of the view.
I know that I can nest them first then do my layout. It also occurs to me that one could leave them as is then add viewA as a subview of viewB in viewDidLoad, viewWillLayoutSubviews, or similar.
However, Is there a way to nest the views in the storyboard without loosing the original layout of viewA
Sorry, I think this was just some weird fluke. I restarted my Mac and it is nesting the views properly now.

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

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.

Resources