【问题标题】:Custom UIView not visible on addSubview自定义 UIView 在 addSubview 上不可见
【发布时间】:2014-03-18 11:21:37
【问题描述】:

我创建了一个简单的 ProgressView 类,我希望在执行一些长任务时显示该类。但是,当我将它添加到父视图时,它永远不会变得可见,并且永远不会调用 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];
}

我添加如下视图:

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

但它从不显示。我已经检查过删除肯定会在以后发生,并且有足够的时间让视图变得可见。然而我不知所措。无论我做什么,ProgressView 都不会显示。

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

【问题讨论】:

  • 您在哪里添加您的视图?问题可能来自这里
  • @NicolasBonnet:谢谢 Nicolas,但我不认为我这样做有什么奇怪的。我是在委托调用内部进行的,我已经确认这发生在主线程上。
  • 我也确认过几秒后,当下载完成时,pProgressView.superview 和 "self.view" 一样...
  • 是不是你的主线程在你添加这个视图之后就被阻塞了,这就是它没有出现的原因?我假设您在添加pProgressView 后正在做某事。尝试将“某物”移至辅助线程。
  • @Levi:不......因为 UIListView 视图应该出现在它上面仍然响应......

标签: ios iphone objective-c uiview uikit


【解决方案1】:

好吧,我想通了,我真的很愚蠢。

我正在使用导航控制器。 NavigtionViewController 处理下载启动。唯一的问题是它是由子视图控制器生成的。所以我将我的 ProgressView 添加到当前不可见的视图中......

D'OH!

对不起。

我新建的addSubview代码如下:

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

【讨论】:

    【解决方案2】:

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

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

    指定的初始化器是调用超类的初始化器的初始化器,更多信息请参见here

    【讨论】:

    • 只是为了澄清这是在你的initWithTitle
    • @Goz - 您是否已在调试器中逐步检查代码以查看标签等是否正确创建?
    • 你在使用约束吗?
    • @ff10: 我怎么知道我是不是?
    • @Goz 尝试在您的 -layoutSubviews 方法中调用 [super layoutSubviews]。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 2018-11-15
    • 2015-03-24
    • 2013-04-18
    相关资源
    最近更新 更多