简体   繁体   English

UIView 自定义 xib 不显示

[英]UIView custom xib doesn't show up

I'm having a problem that doesn't seem to have an obvious solution.我遇到了一个似乎没有明显解决方案的问题。 I've searched around and I've been through all common answers that I could find.我四处搜索,我已经找到了所有我能找到的常见答案。

My custom xib views don't show up on the app when I launch.当我启动时,我的自定义 xib 视图没有显示在应用程序上。 The background is clear, and this xib has 5 image views as you can see below which aren't set to hidden.背景清晰,这个xib 有5 个图像视图,如下所示,它们没有设置为隐藏。

The class has about 5 delegates as well which I set from a caller when the delegates have been initialized.该类还有大约 5 个委托,我在委托初始化时从调用方设置了这些委托。 The caller of initWithDelegates: is the parent UIViewController that displays this xib. initWithDelegates:的调用者是显示这个 xib 的父 UIViewController。

CustomView.h自定义视图.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自定义视图.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

What else I've verified:我还验证了什么:

  1. I made sure that the xib file is properly selected under "Custom Class" in the interface builder.我确保在界面构建器的“自定义类”下正确选择了 xib 文件。
  2. I've walked through the code to make sure there are no crashes or obvious issues.我已经遍历了代码以确保没有崩溃或明显的问题。
  3. The xib doesn't appear to have any obvious issues in interface builder such as being set to HIDDEN. xib 在界面构建器中似乎没有任何明显的问题,例如被设置为 HIDDEN。
  4. The 5 IBOutlet ImageViews get a memory address and aren't nil. 5 个 IBOutlet ImageViews 获得一个内存地址并且不是 nil。

Any help is appreciated!任何帮助表示赞赏!

UPDATE:更新:

I believe this has something to do with putting the nibs in a nested UIView.我相信这与将笔尖放在嵌套的 UIView 中有关。 In the interface builder, the order is UIViewController -> UIView -> nibs在界面生成器中,顺序是 UIViewController -> UIView -> nibs

There doesn't seem to be a method to register nibs from the parent UIViewController as there are in classes like UITableView.似乎没有一种方法可以从父 UIViewController 注册笔尖,就像在 UITableView 这样的类中一样。

I've also tried:我也试过:

  1. Setting the background color in the xib to make sure it shows up.在 xib 中设置背景颜色以确保它显示出来。 It doesn't.它没有。 However if I set the background color to red in the parent where I added the view in interface builder, it turns red.但是,如果我在界面构建器中添加视图的父级中将背景颜色设置为红色,它会变为红色。 ( See Figure 1 ), and my xib file is shown in Figure 2 见图1 ),我的xib文件如图2
  2. Over-rode initWithFrame to make sure its being called.过度使用 initWithFrame 以确保它被调用。 It is called.它被称为。
  3. Thinking that the view may be instantiated twice, one via autolayout and one via my custom init method, I tried making the (id)initWithDelegates method not init or register the nib in case that was duplicating the object.考虑到视图可以实例化两次,一次通过自动布局,一次通过我的自定义 init 方法,我尝试使(id)initWithDelegates方法不初始化或注册 nib 以防复制对象。 I did this by removing 1) return self , 2) the lines that register the nib, and 3) making the method prototype return void - IE (void)initWithDelegates - This didn't work either.我这样做是通过删除 1) return self ,2) 注册笔尖的行,以及 3) 使方法原型返回 void - IE (void)initWithDelegates - 这也不起作用。
  4. I tried these lines within initWithDelegates : [self addSubview: self.image1]; [self bringSubviewToFront: self.image1]; [self.menuImage setHidden:image1];我在 initWithDelegates 中尝试了这些行: [self addSubview: self.image1]; [self bringSubviewToFront: self.image1]; [self.menuImage setHidden:image1]; [self addSubview: self.image1]; [self bringSubviewToFront: self.image1]; [self.menuImage setHidden:image1]; -- with no luck. ——没有运气。

Figure 1: (Actual result)图1:(实际结果) 实际的

Figure 2 (Expected.. with red background)图2(预期..红色背景) 预期的

Check your Autolayout in your custom XIB.在您的自定义 XIB 中检查您的自动布局。 Something the contain does not show because of it.因为它,包含的东西没有显示。 For example: only center vertically & center horizontally.例如:仅垂直居中和水平居中。 Try to do different autolayout constraint in your view in custom class.尝试在自定义类中的视图中执行不同的自动布局约束。

I think something is wrong with layout of your CustomView inside ParentView.我认为您的 CustomView 在 ParentView 中的布局有问题。 Could you please:能不能请你:

  • Check the frame of CustomView when you add it into the ParentView.将 CustomView 添加到 ParentView 时检查它的框架。

  • Check constraints that has CustomView inside ParentView.检查在 ParentView 中具有 CustomView 的约束。 If you don't have them - add it, constraints will iOS the glue how to layout your CustomView.如果您没有它们 - 添加它,约束将 iOS 粘合如何布局您的 CustomView。 More information about this you can find here Programmatically Creating Constraints关于这方面的更多信息,您可以在这里找到以编程方式创建约束

  • And also you can try to use "Debug View Hierarchy" in Xcode to inspect what is going on on your screen, there you can find your CustomView in the view hierarchy and you will see what is wrong.您也可以尝试使用 Xcode 中的“Debug View Hierarchy”来检查屏幕上发生的事情,在那里您可以在视图层次结构中找到您的 CustomView,您将看到什么是错误的。

nibs cannot be nested within the structure.笔尖不能嵌套在结构中。

Instead of:代替:

UIViewController -> UIView -> nibs.. UIViewController -> UIView -> 笔尖..

It should be:它应该是:

UIViewController -> Container View -> UIView with nibs. UIViewController -> Container View -> UIView with nibs。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM