2014-12-20 3 views
0

UIActivityIndicatorView와 메시지를 표시하기 위해 사용자 정의 UIView를 만들었습니다. 아이디어 일관성을 위해 내 애플 리케이션에 걸쳐 이것을 재사용하는 것입니다.사용자 정의 UIView가 스토리 보드에 서브 뷰를 표시하지 않습니다.

@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의 추가, 이것을 시도하고 CDActivityIndicator에 해당 뷰의 클래스를 설정하려면 : 아래는 코드입니다. 해당 View Controller에서 begin()을 호출하면 CDActivityIndicator가 예상 배경색의 빈보기로 표시됩니다. 그러나 활동 표시기는 표시되지 않습니다. 모든 메소드가 예상대로 호출됩니다.

제가 누락 된 것일 수도 있습니다. 감사!

+0

활동 표시기에 대한 전체 코드를 추가 할 수 있습니까? – Sam

답변

1

이렇게 하위보기 프레임 설정을 변경해야합니다.

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

프레임은 수퍼 뷰의 좌표계에서 뷰의 원점과 크기를 정의합니다. 모든 UIView에는 자체 좌표계가 있습니다. 이것을 고려해야합니다.

+0

Perfect! 고마워요. 내 마지막 응용 프로그램이 모든 사용자 정의 레이아웃, NIBs/Storyboard ... 터치의 부족 주어진이 놓친 방법을 모르겠어요 :) – user2373479