ios - Xib 中的 SubViews MainView 为什么不显示在 iOS 中?

标签 ios objective-c xib

问题是View加载了,但是里面的元素没有加载或者没有出现,为什么?

xib 图像是: enter image description here

对应的.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];

问题是什么?

调试器更新:enter image description here

输出是:enter image description here

最佳答案

不幸的是,您不能像这样创建自定义 UIView,它不会起作用。我对可重用的自定义组件使用不同的方式,即:

  1. 在 Storyboard中创建新的 View Controller ,更改其框架并进行自定义。
  2. 给它一个标识符
  3. 在您想要在 Storyboard中使用的所有地方,只需创建容器 View 并嵌入您的自定义 View Controller 。
  4. 如果您想以编程方式使用它。只需使用标识符从 Storyboard 中实例化它。调整其 View 框架的大小并将其添加为 subview 。

关于ios - Xib 中的 SubViews MainView 为什么不显示在 iOS 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37005378/

相关文章:

ios - XIB 和 View Controller 之间的协议(protocol)委托(delegate)

ios - 设置框架未正确调整 UIView 的大小

ios - 使用 `moveRowAtIndexPath:toIndexPath:` 和 `estimatedRowHeight` 时 `UITableViewAutomaticDimension` 中的 UITableViewCell 动画错误

ios - 如何在IOS中缓存以后可以更改的图像?

ios - 保持原始图像大小AFNetworking

objective-c - TWTweetComposeViewController - 动画 GIF

ios - UISearchController.hidesNavigationBarDuringPresentation 在 iOS 11 Beta 中被 scopeButtons 忽略

ios - 在 iOS xCode 项目中是否有组织文件的标准做法?

ios - UIScrollView setContentSize 不起作用

ios - 在 viewDidLoad 之前为手动加载的 Xib 文件提供参数