2012-08-31 66 views
1

我想添加一些UI元素,如UIProgressViewUILabelUIButtonUIActivityIndicator - 我認爲的代碼是正確的(或因此我認爲),但沒有什麼是顯示,除了背景。我究竟做錯了什麼?添加UI元素到自定義的UIView

@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被調用,而推/提出其他視圖循環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如果你建立一個自定義的UIView

+0

當我把代碼放在'viewDidLoad'中時,什麼都沒有出現,甚至沒有背景,當我把它放在'layoutSubviews'中時,它和上面的代碼一樣,沒有任何東西顯示,除了背景。 – Wasim

1

如果您不能添加新的UI元素,以自我的觀點,而不是隻是自我。所以它看起來有點像這樣:

[[self view] addSubview:background]; 

,而不是

[self addSubview:background]; 

編輯

試試這個看控制器上的所有意見。

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

如果您不能訪問self.subviews這意味着你不能添加任何的意見,除了初始的視圖,所以你必須開始添加到[自我視圖。嘗試將self.subviews更改爲[[self view]子視圖];

您的背景視圖可能已被放置爲初始視圖,因爲它是一個UIImageView,嘗試添加到該視圖。

+0

這是一個自定義的UIView,所以我想添加subViews到自己。 Plus不能解釋爲什麼背景顯示,但沒有其他元素顯示。 – Wasim

+0

@Wasim請參閱編輯 – Popeye

+0

沒有'[self view]'代碼位於'UIView'子類而不是'UIViewController'中 - 我嘗試添加NSLog(@「%@」,self.subviews)'at 'initWithFrame'方法的底部,唯一顯示的是背景。所以我嘗試了'[background addSubview:self.activityIndi​​cator]',但仍然沒有顯示出來。 – Wasim

相關問題