2012-05-28 3 views
4

나는 사용자 정의 서브 클래스 UIImageView을 만들었으며 한 가지 방법으로 이미지가로드되는 동안 활동 표시기를 가운데에 추가하려고합니다. 다음 코드를 사용하고 있지만 활동 모니터는 UIImageView 외부에 있습니다. UIImageView 범위 내에서 정확하게 센터를 배치하려면 어떻게해야합니까?UIImageView의 중심 UIActivityIndicatorView

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 

indicator.autoresizingMask = 
UIViewAutoresizingFlexibleTopMargin | 
UIViewAutoresizingFlexibleRightMargin | 
UIViewAutoresizingFlexibleBottomMargin | 
UIViewAutoresizingFlexibleLeftMargin; 

indicator.center = self.center; 

[self addSubview:indicator];  
[indicator startAnimating]; 

답변

13

center 속성은 수퍼의 좌표 공간에서의 좌표도이다. 따라서 imageView의 좌표 공간에서 인디케이터 좌표를 설정해야합니다. 올바른 방법은 다음과 같습니다

indicator.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)); 
+1

완벽하게 작동했습니다. 고마워. – Wasim

2

indicator.center = imageView.center;

3
indicator.center = [self convertPoint:self.center fromView:[self superview]]; 

당신은이 때문에 포인트를 변환 (UIView Class Reference)가 필요합니다 :

@property (세분화) CGPoint 센터 토론

에게 센터가 지정 수퍼 뷰 의 좌표계 내에서 포인트 단위로 측정됩니다.