[英]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:我还验证了什么:
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:我也试过:
(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
- 这也不起作用。[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. ——没有运气。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关于这方面的更多信息,您可以在这里找到以编程方式创建约束
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.