Andrey Chernukha
Andrey Chernukha

Reputation: 21808

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?

Upvotes: 1

Views: 56

Answers (3)

gyan
gyan

Reputation: 219

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.

Upvotes: 1

donnywals
donnywals

Reputation: 7591

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.

Upvotes: 2

dopcn
dopcn

Reputation: 4218

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

Upvotes: 3

Related Questions