为什么我的UIView子视图不在父级内呈现? (附代码) [英] why is my UIView subview not rendering within the parent? (code attached)

查看:190
本文介绍了为什么我的UIView子视图不在父级内呈现? (附代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的自定义 UIView 无法在其父级的 UIView 容器中呈现?所以我拥有的是:

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


  • MainViewController - 有一个 UIView customViewContainer (不占用整个屏幕),

  • CustomView - 是 UIView 带有一个XIB文件 - 这里是 UIView ,当它被呈现时(使用AspectFit)呈现在父的边界之外> customViewContainer

  • 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"

用于设置自定义视图的代码是MainViewController的提取:

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

<< cut - 请参阅下面的Update2 >>

<< cut - see Update2 below >>

所以我看不出为什么 CustomView 视图正在渲染中面积大于父 customViewContainer 的方式?我想要的是 customview 完全符合的父 customViewContainer AspectFit 类型方法。

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.

谢谢

编辑1 (补充说明) - 如果我在父视图中剪辑子视图然后它会剪切内容,但我真正需要在父视图区域内(而不是整个屏幕区域)渲染自定义视图。所以我需要(a)自定义视图的中心位于父视图的中心,以及(b)正确地将AspectFit视图放入父视图中。有什么想法吗?

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?

编辑2 - 更新

抱歉 - 复印/在原始问题中粘贴错误代码 - 似乎无法编辑它所以我将在下面放置一个正确的版本 - 所以澄清:

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 - 有 UIView containerView (不占用整个屏幕)和 CustomView - 是带有XIB文件的 UIView - 它是 UIView 这里呈现它时(使用 AspectFit )呈现在父 containerView

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"

使用下面的代码,现在这有意义吗?这段代码的原因是我有一个自定义 UIView ,但我有一个与之关联的XIB文件,所以这是获取我的的唯一方法MainController 视图能够使用它。也就是说,在 MainController 视图中有一个容器视图,然后以编程方式添加 CustomView 进入容器视图。

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.

重新所以设置你要添加的视图的框架或中心是你想要的是 - 你是说我必须以编程方式/手动设置CustomView的维度是我想要的(与父容器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)?

我希望有一种方法可以使用声明布局设置来说明如何能够说从它的XIB文件加载自定义视图,以及aspectFit这个查看self.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?

更新后的代码(当我复制/粘贴它时在原始问题中犯了错误in和更改变量名称等)

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


推荐答案

检查剪辑子视图 检查IB文件中父项的属性。

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

我认为等效代码是 self.view.clipsToBounds = YES

如果这是,那么在外面绘制的子视图就会被显示,好像它是在父项上绘制的。

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

这篇关于为什么我的UIView子视图不在父级内呈现? (附代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆