问题是View加载了,但是里面的元素没有加载或者没有出现,为什么?
对应的.h
为:
#import <UIKit/UIKit.h>
@interface DownloadView : UIView
@property (strong, nonatomic) IBOutlet UIView *view;
@property (weak, nonatomic) IBOutlet UILabel *downloadFilename;
@property (weak, nonatomic) IBOutlet UILabel *downloadpercentage;
@property (weak, nonatomic) IBOutlet UIProgressView *downloadprogressBar;
@property (weak, nonatomic) IBOutlet UIButton *downloadCancel;
@end
.m
文件是:
#import "DownloadView.h"
@implementation DownloadView
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
[[NSBundle mainBundle] loadNibNamed:@"DownloadView" owner:self options:nil];
[self addSubview:self.view];
}
return self;
}
-(id) initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if(self){
[[NSBundle mainBundle] loadNibNamed:@"DownloadView" owner:self options:nil];
[self addSubview:self.view];
}
return self;
}
调用文件是:
polygonView = [[DownloadView alloc]initWithFrame:CGRectMake(0, height-170, width, 100)];
polygonView.downloadFilename.text = @"Initiating Download...";
[polygonView.downloadprogressBar setProgress:0];
polygonView.downloadpercentage.text = @"0%";
[window addSubview:polygonView];
问题是什么?
最佳答案
不幸的是,您不能像这样创建自定义 UIView,它不会起作用。我对可重用的自定义组件使用不同的方式,即:
- 在 Storyboard中创建新的 View Controller ,更改其框架并进行自定义。
- 给它一个标识符
- 在您想要在 Storyboard中使用的所有地方,只需创建容器 View 并嵌入您的自定义 View Controller 。
- 如果您想以编程方式使用它。只需使用标识符从 Storyboard 中实例化它。调整其 View 框架的大小并将其添加为 subview 。
关于ios - Xib 中的 SubViews MainView 为什么不显示在 iOS 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37005378/