繁体   English   中英

情节提要中的自定义UIView不显示子视图

[英]Custom UIView in storyboard not showing subviews

我创建了一个自定义UIView来显示UIActivityIndi​​catorView和一条消息。 想法是在我的应用程序中重复使用此方法以保持一致性。 下面是代码:

@implementation CDActivityIndicator

@synthesize activityIndicator, message;

//init method called when the storyboard wants to instantiate this view

- (id) initWithCoder:(NSCoder*)coder {

    if ((self = [super initWithCoder:coder])) {

        activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

        message = [[UILabel alloc] initWithFrame:CGRectZero];
        [message setBackgroundColor:[UIColor clearColor]];
        [message setTextColor:[UIColor whiteColor]];
        [message setTextAlignment:NSTextAlignmentCenter];
        [message setFont:[UIFont fontWithName:@"HelveticaNeue" size:15.0f]];

        [self addSubview:activityIndicator];
        [self addSubview:message];

        //set background color
        [self setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]];
    }

    return self;
 }

-(void) layoutSubviews {

    [super layoutSubviews];

    //position the indicator at the center of the view
    [activityIndicator setCenter:[self center]];

    //position the label
    [message setFrame:[self frame]];

}

- (void) begin {

    //make sure the current view is visible, and message is hidden
    [self setHidden:NO];
    [activityIndicator setHidden:NO];
    [self bringSubviewToFront:activityIndicator];
    [message setHidden:YES];

    [self performSelectorOnMainThread:@selector(startAnimating) withObject:self waitUntilDone:YES];
}

- (void) startAnimating {
    if( !activityIndicator.isAnimating ) {
        [activityIndicator startAnimating];
    }
}

为了进行试验,我在情节提要中的一个视图中添加了一个UIView,并将该视图的类设置为CDActivityIndi​​cator。 当我从相应的视图控制器调用begin()时,CDActivityIndi​​cator被显示为具有预期背景颜色的空视图。 但是,活动指示器不显示。 所有方法都按预期方式被调用。

知道我可能会缺少什么吗? 谢谢!

您应该像这样更改子视图框架设置。

CGPoint point = [self.superview convertPoint:self.center toView:self];
[activityIndicator setCenter:point];
[message setFrame:self.bounds];

框架在其超级视图的坐标系中定义视图的原点和尺寸。 每个UIView都有自己的坐标系。 您应该考虑到这一点。

暂无
暂无

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

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