why is my UIView subview not rendering within the parent? (code attached)

谁说我不能喝 提交于 2019-12-30 05:45:06

问题


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];
}

回答1:


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

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.



来源:https://stackoverflow.com/questions/6715341/why-is-my-uiview-subview-not-rendering-within-the-parent-code-attached

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!