簡體   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