Group
Group

Reputation: 335

SubViews MainView in a Xib are not displayed in iOS why?

The problem is View is loaded but the inner elements are not loaded or did not appear why?

the xib image is:enter image description here

and the corresponding .h is:

#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 file is:

#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;
}

The calling file is:

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];

What is the Problem ?

Debugger update:enter image description here

The output is:enter image description here

Upvotes: 1

Views: 398

Answers (1)

Hossam Ghareeb
Hossam Ghareeb

Reputation: 7113

Unfortunately you can't create custom UIView like this, it will not work. I use different way for reusable custom components which is:

  1. Create new view controller in the storyboard, change its frame, and customize it.
  2. Give it an identifier
  3. In all places you want to use in storyboard, just create Container view and embed your custom view controller.
  4. If you want to use it programmatically. Just instantiate it from storyboard with the identifier. Resize its view frame and add it as a subview.

Upvotes: 1

Related Questions