代码之家  ›  专栏  ›  技术社区  ›  TheJeff

UIView自定义xib未显示

  •  -1
  • TheJeff  · 技术社区  · 5 年前

    我遇到了一个似乎没有明显解决办法的问题。我四处搜索,找到了所有常见的答案。

    当我启动应用程序时,我的自定义xib视图不会显示在应用程序上。背景是清晰的,这个xib有5个图像视图,如下所示,它们没有设置为隐藏。

    该类也有大约5个委托,当委托被初始化时,我从调用方设置了这些委托。呼叫者 initWithDelegates:

    CustomView.h

    @interface CustomView : UIView<SomeProtocol>
    
    // UI Items - THESE AREN'T SHOWING UP
    @property (strong, nonatomic) IBOutlet UIImageView *image1;
    @property (strong, nonatomic) IBOutlet UIImageView *image2;
    @property (strong, nonatomic) IBOutlet UIImageView *image3;
    @property (strong, nonatomic) IBOutlet UIImageView *image4;
    @property (strong, nonatomic) IBOutlet UILabel *label;
    
    @property (strong, nonatomic) IBOutlet UIStackView *centerStackView;
    
    - (id)initWithDelegates:(UIViewController*)delegate1 withDelegate2:(NSObject<Delegate2>*)delegate2 withDelegate3:(NSObject<Delegate3>*)delegate3 withDelegate4:(UIViewController<Delegate4>*)delegate4
        withDelegate5:(NSObject<Delegate5>*)delegate5;
    
    @end
    

    CustomView.m

    @implementation CustomView
    
    - (void)awakeFromNib {
        [super awakeFromNib];
    }
    
    - (id)initWithDelegates:(UIViewController*)delegate1 withDelegate2:(NSObject<Delegate2>*)delegate2 withDelegate3:(NSObject<Delegate3>*)delegate3 withDelegate4:(UIViewController<Delegate4>*)delegate4
        withDelegate5:(NSObject<Delegate5>*)delegate5
    {
    
        NSArray * arr =[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:nil options:nil];
        self = [arr firstObject];
    
    
        self.delegate1 = delegate1;
        self.delegate2 = delegate2;
        self.delegate3 = delegate3;
        self.delegate4 = delegate4;
        self.delegate5 = delegate5;
    
        [self setLoopImage];
    
        [self layoutIfNeeded];
    
        return self;
    }
    @end
    

    1. 我已经仔细检查了代码,以确保没有崩溃或明显的问题。
    2. 5个IBMOutlet ImageView获得一个内存地址,并且不是零。

    我相信这与将笔尖放在嵌套的UIView中有关。在界面生成器中,顺序为UIViewController->UIView->笔尖

    似乎没有一种方法可以从父UIViewController注册NIB,就像UITableView这样的类中一样。

    我也试过:

    1. 在xib中设置背景色以确保其显示。没有。但是,如果在interface builder中添加视图的父级中将背景色设置为红色,则背景色将变为红色( 参见图1 ),我的xib文件显示在中 图2
    2. 在initWithFrame上单击以确保调用它。它被称为。
    3. (id)initWithDelegates 方法不初始化或注册nib,以防复制对象。为此,我删除了1) return self ,2)注册nib的行,3)使方法原型返回void-即。 (void)initWithDelegates
    4. 我在initWithDelegates中尝试了以下几行: [self addSubview: self.image1]; [self bringSubviewToFront: self.image1]; [self.menuImage setHidden:image1]; -- 没有运气。

    图1:(实际结果) Actual

    图2(预期)。。(背景为红色) Expected

    0 回复  |  直到 5 年前
        1
  •  0
  •   Kai    5 年前

        2
  •  0
  •   Roman Tsymbaliuk    5 年前

    我认为您的CustomView在ParentView中的布局有问题。请你:

    • 将CustomView添加到父视图时,请检查其框架。

    • 有了它们——加上它,约束将成为如何布局的粘合剂 自定义视图。更多关于这方面的信息可以在这里找到 Programmatically Creating Constraints

    • "Debug View Hierarchy" 在Xcode中 检查屏幕上正在发生的事情,在那里你可以找到你的
        3
  •  0
  •   TheJeff    3 年前

    NIB不能嵌套在结构中。

    而不是:

    UIViewController->UIView->笔尖。。

    应该是:

    UIViewController->容器视图->带笔尖的UIView。