gpt4 book ai didi

ios - 自定义 UIView 在 addSubview 上不可见

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:06:29 25 4
gpt4 key购买 nike

我创建了一个简单的 ProgressView 类,我希望在执行一些长任务时显示它。但是,当我将它添加到父 View 时,它永远不会变得可见,并且永远不会调用 layoutSubviews。

类实现如下:

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

然后我添加如下 View :

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

但它永远不会显示。我已经检查过删除肯定会在稍后发生,并且有足够的时间让 View 可见。但是我很茫然。无论我做什么,ProgressView 都不会显示。

有人可以指出我正在犯的最有可能是愚蠢的错误吗?

最佳答案

需要在-initWithTitle中调用指定的初始化器,而不是[super init]:

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

指定的初始化器是调用父类(super class)初始化器的那个,参见here了解更多信息。

关于ios - 自定义 UIView 在 addSubview 上不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21858292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com