为什么我的 UIView 子视图没有在父视图中渲染? (附代码)

发布于 2024-11-24 11:49:47 字数 2227 浏览 0 评论 0原文

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

  • MainViewController - 有一个 UIViewcustomViewContainer”(不占用整个屏幕),而
  • CustomView - 是一个 UIView< /code> 带有 XIB 文件 - 这是这里的 UIView,当它被渲染(使用 AspectFit)时,它会在父级“customViewContainer”的边界之外渲染

使用的代码设置自定义视图是这样的从 MainViewController 中提取:

<<剪切-参见下面的更新2>>

所以我不明白为什么 CustomView 视图的渲染方式比父 customViewContainer 的面积更大?我想要的是 customview 完全按照 AspectFit 类型方法适合父“customViewContainer”。

谢谢

编辑1(添加说明)-如果我在父视图中“剪辑子视图”,那么它就会剪辑内容,但我真正需要在父视图区域中渲染自定义视图(而不是屏幕的整个区域)。因此,我需要 (a) 自定义视图的中心位于父视图的中心,以及 (b) 自定义视图以 AspectFit 正确融入父视图。有什么想法吗?

编辑2 - 更新

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

MainViewController - 有一个 UIViewcontainerView”(不占据整个屏幕),而 CustomView - 是一个UIView 带有XIB 文件 - 这里是 UIView,当它被渲染(使用 AspectFit)时,它会在父“containerView”的边界之外

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

回复“因此,将要添加的视图的框架或中心设置为您想要的位置”-您是说我必须以编程方式/手动将 CustomView 的尺寸设置为我想要的尺寸(相对于父容器视图)?

我希望有一种方法可以使用声明性布局设置来表达“从 XIB 文件加载自定义视图,并将该视图放入 self.containerView 中”,但是我开始怀疑如果这可能吗?

下面更新了代码(当我复制/粘贴并更改变量名称等时,在原始问题中犯了错误)

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不知在何时 2024-12-01 11:49:47

检查 IB 文件上父级的“剪辑子视图”属性是否已选中。

Clip Subviews

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

如果这是NO,则在外部绘制的子视图将可见,就像在父视图上绘制一样。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文