我想添加一些UI元素,如UIProgressView
,UILabel
,UIButton
和UIActivityIndicator
- 我認爲的代碼是正確的(或因此我認爲),但沒有什麼是顯示,除了背景。我究竟做錯了什麼?添加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];
}
你可以顯示d代碼,爲什麼你調用這個mthd並在子視圖上添加.. – vishy
@vishy我已經發布了代碼。 – Wasim
我試了相同的代碼,成功地添加到視圖..如何訪問此視圖控制器,通過推/呈現...因爲雖然分配一個控制器viewDidLoad被調用,而推/提出其他視圖循環mthds將被稱爲.. – vishy