Should a subView ever go outside the bounds of its superview


#1

lookng at the code from the book :

[code]
CGRect viewFrame = CGRectMake(160, 240, 100, 150);
HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
[view setBackgroundColor:[UIColor redColor]];
[[self window] addSubview:view];

CGRect anotherFrame = CGRectMake(120, 130, 50, 50);
HypnosisView *anotherView = [[HypnosisView alloc] initWithFrame:anotherFrame];
[anotherView setBackgroundColor:[UIColor blueColor]];
//[[self window] addSubview:anotherView];
[view addSubview:anotherView];[/code]

In this case ‘anotherView’ is drawn OUTSIDE (the bounds) its superview ‘view’. I am sure this is “working as intended” but to me it doesnt make sense. A subView, in my mind, should never be outside the bounds of its superView in a parent->child relationship but in this case it is happening. Any thoughts about this?


#2

In iOS, view clipping works differently.

Here is an excerpt from Adding and Removing Subviews, View Programming Guide for iOS:


#3

[quote=“ibex10”]In iOS, view clipping works differently.

Here is an excerpt from Adding and Removing Subviews, View Programming Guide for iOS:

[quote]
When adding a subview to its parent, the subview’s current frame rectangle denotes its initial position inside the parent view. A subview whose frame lies outside of its superview’s visible bounds is not clipped by default. If you want your subview to be clipped to the superview’s bounds, you must explicitly set the clipsToBounds property of the superview to YES.
[/quote][/quote]

thanks a bunch! Just as a thought this maybe would have been something to add to this chapter - but I know that it would be impossible to add everything. As I go through the chapter I always ask myself, “what would happen if i do this?” or “what if I did that?” to find out if I can learn anything beyond just the books material. thanks again for the info ibex10.