UIView 自定义 xib 不显示
UIView custom xib doesn't show up
我遇到了一个似乎没有明显解决方案的问题。我四处搜索,找到了所有常见的答案。
我的自定义 xib 视图在我启动时没有显示在应用程序上。背景很清晰,这个 xib 有 5 个图像视图,如下所示,没有设置为隐藏。
class 也有大约 5 个委托,这是我在委托初始化后从调用者那里设置的。 initWithDelegates:
的调用者是显示这个xib的父UIViewController。
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
我还验证了什么:
- 我确保在界面生成器中 "Custom Class" 下正确选择了 xib 文件。
- 我已经检查了代码以确保没有崩溃或明显的问题。
- xib 在界面生成器中似乎没有任何明显的问题,例如被设置为隐藏。
- 5 个 IBOutlet ImageView 获取内存地址并且不是 nil。
感谢任何帮助!
更新:
我相信这与将笔尖放入嵌套的 UIView 中有关。在界面生成器中,顺序是 UIViewController -> UIView -> nibs
似乎没有从父 UIViewController 注册 nib 的方法,就像 classes 中那样,如 UITableView。
我也试过:
- 正在 xib 中设置背景颜色以确保它显示出来。它没有。但是,如果我在 Interface Builder 中添加视图的父项中将背景颜色设置为红色,它会变成红色。 (见图1),而我的xib文件如图图2
- Over-rode initWithFrame 以确保它被调用。它被称为。
- 认为视图可能被实例化两次,一次通过自动布局,一次通过我的自定义 init 方法,我尝试使
(id)initWithDelegates
方法不初始化或注册笔尖以防复制对象。我通过删除 1) return self
、2) 注册笔尖的行和 3) 使方法原型 return void - I.E. (void)initWithDelegates
- 这也没有用。
- 我在 initWithDelegates 中尝试了这些行:
[self addSubview: self.image1];
[self bringSubviewToFront: self.image1];
[self.menuImage setHidden:image1];
-- 运气不好。
图一:(实际结果)
图 2(预期..红色背景)
检查自定义 XIB 中的自动版式。包含的东西因此而没有显示。例如:仅垂直居中和水平居中。尝试在自定义 class.
视图中执行不同的自动布局约束
我认为您的 CustomView 在 ParentView 中的布局有问题。可以请你:
将CustomView添加到ParentView时检查其框架
检查在 ParentView 中有 CustomView 的约束。如果你不
有它们 - 添加它,约束将 iOS 胶水如何布局你的
自定义视图。您可以在此处找到更多相关信息 Programmatically Creating Constraints
- 你也可以尝试在Xcode中使用"Debug View Hierarchy"来
检查屏幕上发生了什么,在那里你可以找到你的
视图层次结构中的 CustomView,您将看到错误所在。
nib 不能嵌套在结构中。
而不是:
UIViewController -> UIView -> nibs..
应该是:
UIViewController -> 容器视图 -> 带有 nib 的 UIView。
我遇到了一个似乎没有明显解决方案的问题。我四处搜索,找到了所有常见的答案。
我的自定义 xib 视图在我启动时没有显示在应用程序上。背景很清晰,这个 xib 有 5 个图像视图,如下所示,没有设置为隐藏。
class 也有大约 5 个委托,这是我在委托初始化后从调用者那里设置的。 initWithDelegates:
的调用者是显示这个xib的父UIViewController。
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
我还验证了什么:
- 我确保在界面生成器中 "Custom Class" 下正确选择了 xib 文件。
- 我已经检查了代码以确保没有崩溃或明显的问题。
- xib 在界面生成器中似乎没有任何明显的问题,例如被设置为隐藏。
- 5 个 IBOutlet ImageView 获取内存地址并且不是 nil。
感谢任何帮助!
更新:
我相信这与将笔尖放入嵌套的 UIView 中有关。在界面生成器中,顺序是 UIViewController -> UIView -> nibs
似乎没有从父 UIViewController 注册 nib 的方法,就像 classes 中那样,如 UITableView。
我也试过:
- 正在 xib 中设置背景颜色以确保它显示出来。它没有。但是,如果我在 Interface Builder 中添加视图的父项中将背景颜色设置为红色,它会变成红色。 (见图1),而我的xib文件如图图2
- Over-rode initWithFrame 以确保它被调用。它被称为。
- 认为视图可能被实例化两次,一次通过自动布局,一次通过我的自定义 init 方法,我尝试使
(id)initWithDelegates
方法不初始化或注册笔尖以防复制对象。我通过删除 1)return self
、2) 注册笔尖的行和 3) 使方法原型 return void - I.E.(void)initWithDelegates
- 这也没有用。 - 我在 initWithDelegates 中尝试了这些行:
[self addSubview: self.image1]; [self bringSubviewToFront: self.image1]; [self.menuImage setHidden:image1];
-- 运气不好。
图一:(实际结果)
图 2(预期..红色背景)
检查自定义 XIB 中的自动版式。包含的东西因此而没有显示。例如:仅垂直居中和水平居中。尝试在自定义 class.
视图中执行不同的自动布局约束我认为您的 CustomView 在 ParentView 中的布局有问题。可以请你:
将CustomView添加到ParentView时检查其框架
检查在 ParentView 中有 CustomView 的约束。如果你不 有它们 - 添加它,约束将 iOS 胶水如何布局你的 自定义视图。您可以在此处找到更多相关信息 Programmatically Creating Constraints
- 你也可以尝试在Xcode中使用"Debug View Hierarchy"来 检查屏幕上发生了什么,在那里你可以找到你的 视图层次结构中的 CustomView,您将看到错误所在。
nib 不能嵌套在结构中。
而不是:
UIViewController -> UIView -> nibs..
应该是:
UIViewController -> 容器视图 -> 带有 nib 的 UIView。