简体   繁体   English

自定义UIView在addSubview上不可见

[英]Custom UIView not visible on addSubview

I have created a simple ProgressView class that I want to be shown when I'm doing some long tasks. 我创建了一个简单的ProgressView类,当我执行一些长任务时,希望显示该类。 However when I add it to the parent view it never becomes visible and layoutSubviews is never called. 但是,当我将其添加到父视图时,它永远不会变得可见,也不会调用layoutSubviews。

The class is implemented as follows: 该类的实现如下:

@implementation ProgressView

@synthesize title;
@synthesize cancel;
@synthesize progress;
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    //frame = CGRectMake( 0, 0, 240, 112 );
    self = [super initWithFrame:frame];
    if ( self )
    {
        self.title      = [[UILabel alloc] init];
        self.cancel     = [UIButton buttonWithType: UIButtonTypeCustom];
        self.progress   = [[UIProgressView alloc] init];

        self.backgroundColor    = [UIColor darkGrayColor];

        [self addSubview: self.title];
        [self addSubview: self.cancel];
        [self addSubview: self.progress];
    }
    return self;
}

+ (id) createWithTitle: (NSString*) pTitle
{
    ProgressView* pRet  = [[ProgressView alloc] initWithTitle: pTitle];
    return pRet;
}

- (id) initWithTitle: (NSString*) pTitle
{
    self = [super initWithFrame: CGRectMake( 0, 0, 240, 112 )];
    if ( self )
    {
        self.title.text = pTitle;
    }
    return self;
}

- (void) layoutSubviews
{
    CGSize superFrameSize       = self.superview.frame.size;
    CGSize halfSuperFrameSize   = CGSizeMake( superFrameSize.width / 2, superFrameSize.height / 2 );

    CGSize frameSize            = self.frame.size;
    CGSize halfFrameSize        = CGSizeMake( frameSize.width / 2, frameSize.height / 2 );

    // Center the window on its parent.
    [self setFrame: CGRectMake( halfSuperFrameSize.width - halfFrameSize.width, halfSuperFrameSize.height - halfFrameSize.height, frameSize.width, frameSize.height )];

    [self.title     setFrame: CGRectMake( 4,  4, frameSize.width - 8, 24 )];
    [self.progress  setFrame: CGRectMake( 4, 32, frameSize.width - 8, 24 )];
    [self.cancel    setFrame: CGRectMake( 4, 64, frameSize.width - 8, 44 )];

    [self setNeedsDisplay];
}

And I add the view as follow: 我将视图添加如下:

pProgressView   = [[ProgressView alloc] initWithTitle: @"Downloading..."];
[pProgressView setDelegate: self];
[self.view addSubview: pProgressView];

But it never displays. 但它永远不会显示。 I've checked the remove is definitely happening later and there is plenty of time for the view to become visible. 我已经检查过删除操作的确会在以后发生,并且视图有很多时间变得可见。 I'm at a loss however. 但是我很茫然。 Whatever I do the ProgressView just won't display. 无论我做什么,ProgressView都不会显示。

Can someone point out the, most probably stupid, mistake I'm making? 有人可以指出我正在犯的(很可能是愚蠢的)错误吗?

You need to call the designated initializer in -initWithTitle, not [super init]: 您需要在-initWithTitle中而不是[super init]中调用指定的初始化程序:

self = [self initWithFrame: CGRectMake( 0, 0, 240, 112 )];

The designated initializer is the one which calls the superclass' initializer, see here for more info. 指定的初始化程序是调用超类的初始化程序的初始化程序,有关更多信息,请参见此处

Ok I figured it, and I'm REALLY stupid. 好吧,我想通了,我真的很傻。

I am using a navigation controller. 我正在使用导航控制器。 And the NavigtionViewController handles the download initiation. 然后NavigtionViewController处理下载启动。 Only problem is that its being generated by a child view controller. 唯一的问题是它是由子视图控制器生成的。 So I'm adding my ProgressView to a view that is currently not visible ... 所以我要将ProgressView添加到当前不可见的视图中...

D'OH! D'OH!

Sorry all. 不好意思

My new addSubview code is as follows: 我的新addSubview代码如下:

[self.navigationController.topViewController.view addSubview: pProgressView];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM