gpt4 book ai didi

ios - subview 不显示按钮或 imageView

转载 作者:行者123 更新时间:2023-11-29 02:35:47 24 4
gpt4 key购买 nike

作为另一个 View 的 subview 呈现的 View 在 .Xib 文件中具有以下设置。 enter image description here

以下是当触摸呈现 View 的按钮时 View 的呈现方式。 enter image description here

这里是如何呈现 View 的代码。

-(void)setUpImageSelectorView{
ImageSelectorViewViewController *imageSelectorVC = [[ImageSelectorViewViewController alloc]initWithNibName:@"ImageSelectorViewViewController" bundle:nil];
imageSelectorVC.delegate = self;
imageSelectorVC.view.contentMode = UIViewContentModeScaleAspectFit;
imageSelectorVC.view.clipsToBounds = YES;
imageSelectorVC.imageView.image = img;
[self.view addSubview:imageSelectorVC.view];
}

最佳答案

两个问题:

  1. 这不是添加具有自己的 Controller 的 subview 的正确代码。您应该使用自定义容器调用,例如:

    -(void)setUpImageSelectorView{
    ImageSelectorViewViewController *imageSelectorVC = [[ImageSelectorViewViewController alloc]initWithNibName:@"ImageSelectorViewViewController" bundle:nil];
    [self addChildViewController:imageSelectorVC];
    imageSelectorVC.delegate = self;
    imageSelectorVC.view.contentMode = UIViewContentModeScaleAspectFit;
    imageSelectorVC.view.clipsToBounds = YES;
    imageSelectorVC.imageView.image = img;
    [self.view addSubview:imageSelectorVC.view];
    // you probably should set the `frame` of this view, too
    [imageSelectorVC didMoveToParentViewController:self];
    }

    注意 addChildViewControllerdidMoveToParentViewController 调用。

    当您要删除它时,首先调用 [imageSelectorVC willMoveToParentViewController:nil],当您完成删除所有内容时,调用 [self removeChildViewController: imageSelectorVC]

    参见 Creating Custom Container View ControllersiOS 的 View Controller 编程指南中。要讨论为什么所有这些都如此重要(即,使您的 View 层次结构与 View Controller 层次结构保持同步),请参见 WWDC 2011 视频 Implementing UIViewController Containment .

  2. 如果您通过调试器运行应用程序并暂停执行(点击暂停按钮,而不是停止按钮),您可以通过键入确认 View 的布局

    (lldb) po [[UIWindow keyWindow] recursiveDescription]
    <UIWindow: 0x7fd91b542420; frame = (0 0; 375 667); gestureRecognizers = <NSArray: 0x7fd91b543000>; layer = <UIWindowLayer: 0x7fd91b5412d0>>
    | <UIView: 0x7fd91b5442d0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x7fd91b5435c0>>
    | | <_UILayoutGuide: 0x7fd91b5445e0; frame = (0 0; 0 20); hidden = YES; layer = <CALayer: 0x7fd91b537030>>
    | | <_UILayoutGuide: 0x7fd91b544f40; frame = (0 667; 0 0); hidden = YES; layer = <CALayer: 0x7fd91b543850>>
    | | <UIButton: 0x7fd91b446d50; frame = (5 285; 70 30); opaque = NO; tag = 12221; layer = <CALayer: 0x7fd91b4d8fb0>>
    | | | <UIImageView: 0x7fd91b57aa20; frame = (0 0; 70 30); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7fd91b53b6f0>>
    | | | <UIImageView: 0x7fd91b51c040; frame = (0 0; 0 0); clipsToBounds = YES; hidden = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7fd91b539670>>

    有时 View 在那里,但如果它们的宽度/高度为零(尤其是当您裁剪到边界时),您将看不到它。

关于ios - subview 不显示按钮或 imageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26393191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com