17

Why is my custom UIView not rendering within the UIView container of it's parent? So what I have is:

  • MainViewController - has a UIView "customViewContainer" (which doesn't take up the whole screen), and
  • CustomView - is a UIView with a XIB file - it is the UIView here that when it is rendered (with AspectFit) is rendering outside the bounds of the parent "customViewContainer"

The code used to setup the custom view is this extract from MainViewController:

<< cut - see Update2 below >>

So I can't see why the CustomView view is being rendered in a way that is larger in area than the parent customViewContainer? What I want is for the customview to fit into the parent "customViewContainer" entirely per the AspectFit type approach.

thanks

EDIT 1 (added clarification) - If I "clip subviews" in the parent view then it does then clip things, but what I really need to render the custom view within the parent view area (not the whole area of the screen). So I need (a) the center of the custom view to be in the center of the parent view, and (b) custom view to AspectFit into the parent view properly. Any ideas?

EDIT 2 - Update

sorry - made a copy/paste mistake with code in the original question - can't seem to edit it so I'll put a correct version below - so to clarify:

MainViewController - has a UIView "containerView" (which doesn't take up the whole screen), and CustomView - is a UIView with a XIB file - it is the UIView here that when it is rendered (with AspectFit) is rendering outside the bounds of the parent "containerView"

With the code below does this make sense now? The reason for this code is I have a custom UIView, BUT I have a XIB file associated with it, so this was the only way to get my MainController view to be able to use it. That is, have a container view in the MainController view, and then programmatically add the CustomView into the container view.

Re "So set the frame or center of the view that you're adding to be what you want it to be" - are you saying I have to programmatically/manually set the dimension of the CustomView to be what I want (in relation to the parent containerView)?

What I was hoping was there was a way using the declaritive layout setting to some how be able to say "Load the Custom View from it's XIB file, and the aspectFit this view into the self.containerView", however I'm starting to wonder if this is possible?

UPDATED CODE BELOW (made mistake in original question when I copy/pasted it in and changed variables names etc)

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Load the custom Altimeter View into this UIControllerView's container UIView for it
    NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"Customview" owner:self options:nil];
    for (NSObject *obj in nibs) {
        if ( [obj isKindOfClass:[Customview class]]) {
            Customview *cv = (Customview*)obj;
            [self.containerView addSubview:cv];
            break;
        }
    }
    // UI Layout
    self.containerView.layer.borderWidth = 2;
    self.containerView.layer.borderColor = [[UIColor redColor] CGColor];
}
Nischal Hada
  • 3,230
  • 3
  • 27
  • 57
Greg
  • 34,042
  • 79
  • 253
  • 454

1 Answers1

24

Check if the "Clip subviews" property of parent on the IB file is checked.

Clip Subviews

I think the equivalent code is self.view.clipsToBounds = YES.

if this is NO, subviews that draws outside will be visible as if it's drawn on the parent.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Mugunth
  • 14,461
  • 15
  • 66
  • 94
  • thanks - the problem is if I "clip subviews" it does then clip things, but what I really need to render the custom view within the parent view area (not the whole area of the screen). So I need (a) the center of the custom view to be in the center of the parent view, and (b) custom view to AspectFit into the parent view properly. Any ideas? – Greg Jul 16 '11 at 08:15
  • Can you be a bit more specific? I didn't get what you meant here. self.view.center = self.parentView.center should center the view. For AspectFit, I'm not sure if there is a built in property. If I'm not wrong, it's available for UIImageView only – Mugunth Jul 16 '11 at 15:46
  • thanks for persisting - I've updated/clarified things more in my EDIT2 in the main question – Greg Jul 17 '11 at 02:15
  • Yes, you have to manually position it to the center of parent. "declarative way" of positioning is available for all four corners but not center. Play around with the autoresizingMasks. For "Aspect Fit" a custom view, you have to write code manually. Some logic on how to aspect fit a CoreGraphics rendered view here. http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/ – Mugunth Jul 17 '11 at 13:49
  • Thanks man. It really helps me. But i dont understand what is doing this. What is the meaning of clip? – Eladar Mar 04 '17 at 16:20