2012-08-31 4 views
1

UIProgressView, UILabel, UIButtonUIActivityIndicator과 같은 UI 요소를 추가하려고합니다. 코드가 정확하다고 생각 하긴하지만 배경을 제외하고는 아무 것도 보이지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?사용자 정의 UIView에 UI 요소 추가하기

@interface LoadingView : UIView 

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator; 
@property (weak, nonatomic) IBOutlet UIProgressView *progressView; 
@property (weak, nonatomic) IBOutlet UILabel *statusLabel; 
@property (weak, nonatomic) IBOutlet UIButton *cancelButton; 

- (void)removeLoadingView; 

@end 


@implementation LoadingView 

@synthesize activityIndicator = _activityIndicator; 
@synthesize progressView = _progressView; 
@synthesize statusLabel = _statusLabel; 
@synthesize cancelButton = _cancelButton; 

- (UIView *)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 

    if (self) { 
     self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(34, 225, 248, 9)]; 
     self.statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(36, 257, 248, 31)]; 
     self.cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(121, 317, 74, 37)]; 

     // Create a new image view, from the image made by our gradient method 
     UIImageView *background = [[UIImageView alloc] initWithImage:[self addBackground]]; 
     background.alpha = 0.8; 
     [self addSubview:background]; 

     self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
     // Set the resizing mask so it's not stretched 
     self.activityIndicator.autoresizingMask = 
     UIViewAutoresizingFlexibleTopMargin | 
     UIViewAutoresizingFlexibleRightMargin | 
     UIViewAutoresizingFlexibleBottomMargin | 
     UIViewAutoresizingFlexibleLeftMargin; 

     CGRect indicatorFrame = CGRectMake(142, 163, 37, 37); 
     [self.activityIndicator setFrame:indicatorFrame]; 

     self.statusLabel.text = @"test 123"; 
     self.statusLabel.hidden = NO; 
     self.statusLabel.enabled = YES; 
     self.statusLabel.textColor = [UIColor whiteColor]; 
     [self addSubview:self.activityIndicator]; 
     [self addSubview:self.progressView]; 
     [self addSubview:self.cancelButton]; 
     [self addSubview:self.statusLabel]; 

     [self.activityIndicator startAnimating]; 

     /* // Create a new animation 
     CATransition *animation = [CATransition animation]; 
     // Set the type to a nice wee fade 
     [animation setType:kCATransitionFade]; 
     // Add it to the superView 
     [[super layer] addAnimation:animation forKey:@"layerAnimation"];*/ 

    } 
    return self; 
} 



- (UIImage *)addBackground{ 
    // Create an image context (think of this as a canvas for our masterpiece) the same size as the view 
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 1); 
    // Our gradient only has two locations - start and finish. More complex gradients might have more colours 
    size_t num_locations = 2; 
    // The location of the colors is at the start and end 
    CGFloat locations[2] = { 0.0, 1.0 }; 
    // These are the colors! That's two RBGA values 
    CGFloat components[8] = { 
     0.4,0.4,0.4, 0.8, 
     0.1,0.1,0.1, 0.5 }; 
    // Create a color space 
    CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB(); 
    // Create a gradient with the values we've set up 
    CGGradientRef myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations); 
    // Set the radius to a nice size, 80% of the width. You can adjust this 
    float myRadius = (self.bounds.size.width*.8)/2; 
    // Now we draw the gradient into the context. Think painting onto the canvas 
    CGContextDrawRadialGradient (UIGraphicsGetCurrentContext(), myGradient, self.center, 0, self.center, myRadius, kCGGradientDrawsAfterEndLocation); 
    // Rip the 'canvas' into a UIImage object 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    // And release memory 
    CGColorSpaceRelease(myColorspace); 
    CGGradientRelease(myGradient); 
    UIGraphicsEndImageContext(); 
    // … obvious. 
    return image; 
} 

편집 : 그래서 난 그냥 viewDidload에 코드를 넣어 아무것도조차 배경, 지금은 표시되지 않습니다. 나는 또한 코드를 위의 코드와 동일한 결과로 layoutSubviews에 시도했지만 백그라운드를 제외한 모든 것은 숨겨져 있습니다.

보기 컨트롤러에서이보기를 인스턴스화하는 데 사용하는 코드는 다음과 같습니다.

@property (nonatomic, strong) LoadingView *loadingView; 

@synthesize loadingView = _loadingView; 

- (LoadingView *)loadingView 
{ 
    if (!_loadingView) _loadingView = [[LoadingView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    return _loadingView; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.view addSubview:self.loadingView]; 
} 
+0

d 코드를 보여줄 수 있습니까?이 mthd를 호출하고 서브 뷰에 추가하는 이유는 무엇입니까? – vishy

+0

@vishy 코드를 게시했습니다. – Wasim

+0

나는보기에 성공적으로 추가 된 동일한 코드를 시도했다 ..이 뷰 컨트롤러에 억세스/프리젠 테이션으로 접근하는 방법 ...왜냐하면 컨트롤러의 viewDidLoad가 호출되기 때문에 & push/present 다른 뷰 사이클 mthds가 호출 될 것이기 때문입니다. – vishy

답변

2

@interface 코드를 게시 한 후에야 weak 포인터가있는 요소를 생성하고 있음을 알았습니다. 나는 그것을 strong으로 바꿨고 모든 것이 잘 보였다. 답변을 주신 모든 분들께 감사드립니다. 전체 코드를 표시하지 않음으로써 모든 것을 혼동 시켰습니다.

+0

당신이 그것을 정렬 알고 있습니다. – Popeye

3

보기 컨트롤러의 viewDidLoad에 넣으십시오. 이렇게하면 기본보기가 이미 있는지 확인할 수 있습니다.

+0

'viewDidLoad'에 코드를 넣으려고해도 아무 것도 나타나지 않습니다 ... 배경이 너무 이상합니다. – Wasim

+0

@Mundi가 코드를'viewDidLoad'에 넣기 위해 언급했는지 확인하십시오. 그렇게하면,'initWithFrame' 메쏘드의 if 문 사이에서 코드 스 니펫을 가져 와서'viewDidLoad'에 붙이십시오. – Sal

1

원하는 장소에서 initWithFrame: 메서드를 호출 했습니까?

난 당신이 더 나은 방법 init: 또는 -ViewDidLoad에 코드를 추가 거라고 생각

1

해보십시오 self.cancelButton.backgroundColor = [UIColor redColor]; 그리고 어떤 것이 나타나는지보십시오.

기본적으로 코드로 작성된 사용자 정의 단추에는 배경 이미지 나 색상이 없으므로 수동으로 지정해야합니다.

+0

색상이 변경되었지만 여전히 아무것도 표시되지 않습니다. – Wasim

1

당신은 viewDidLoad에서, 또는 - (void)layoutSubviews에 그냥 자기에 당신이 자기의보기에 새 UI 요소를 추가 할 수 없습니다해야 커스텀 UIView의

+0

'viewDidLoad'에 코드를 넣을 때 배경이 아닌 아무것도 보이지 않습니다. 위의 코드와 같은 'layoutSubviews'에 넣을 때 아무것도 보이지 않고 아무것도 보이지 않습니다. – Wasim

1

을 구축하지 않는 경우 그렇게해야한다.

이 컨트롤러의 모든 의견을 참조하십시오

[[self view] addSubview:background]; 

하지

[self addSubview:background]; 

편집 : 그래서 그것은이 같은 작은 선물을 보일 것이다. 당신이 self.subviews에 액세스 할 수없는 경우

for(UIView *vw in self.subviews) { 
     NSLog(@"View : %@", vw); 
    } 

이 당신이 [자기보기]에 추가 시작해야하므로 초기보기 제외한 모든보기를 추가 할 수 없음을 의미합니다. self.subviews를 [[self view] subviews]로 변경해보십시오.

배경보기가 UIImageView이므로 초기보기로 배치되었을 수 있습니다. 추가해보십시오.

+0

글쎄, 그건 사용자 정의 UIView, 그래서 자체에 하위보기를 추가하고 싶습니다. Plus가 배경이 표시되지만 다른 요소가 표시되지 않는 이유를 설명하지 않습니다. – Wasim

+0

@Wasim 편집을 참조하십시오 – Popeye

+0

'UIViewController'가 아닌'[self view]'코드가'UIView' 서브 클래스에 있습니다 - NSLog (@ "% @", self.subviews)'를 추가하려고했습니다. 'initWithFrame' 메쏘드의 밑바닥과 배경 만 보여줍니다. 그래서 나는'[background addSubview : self.activityIndicator]'를 시도했지만 여전히 아무것도 나타나지 않는다. – Wasim

관련 문제