Displaying UIActivityIndicatorView until UIView is loaded - ios

In my UIViewController's loadView method I start a UIActivityIndicator. Later in loadView I load a complex UIView, which takes about 2-3 secs to load. (UIScrollView with a lot of pictures more specifically). My problem is that the UIActivityIndicator spinner is only visible, after my other layer has loaded too. (which is of course useless to me). What is a correct way to handle this?
- (void)loadView {
CGRect fullScreenRect=[[UIScreen mainScreen] bounds];
UIView *contentView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 340, fullScreenRect.size.width)]autorelease];
self.view = contentView;
spinner = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
spinner.center = self.view.center;
[self.view addSubview:spinner];
[spinner startAnimating];
scrollView=[[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 340, fullScreenRect.size.width)]autorelease];
...setting up scrollView...
[self.view addSubview:scrollView];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[spinner removeFromSuperview];
}
I have found a somewhat similar thread:
UIActivityIndicatorView not showing until after loading done
But it suggests to load things in a background thread, but loading and displaying a UIView is only possible in the main thread as far as I know.
I am a beginner, and sorry if my question is fundamentally wrong.

I managed to make it the way Carl Veazey suggested. It was pretty easy actually. I spawned a new background thread, and loaded my UIScrollView there.
I used, in LoadView:
spinner = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
spinner.center = self.view.center;
[self.view addSubview:spinner];
[spinner startAnimating];
[self performSelectorInBackground:#selector(populateScrollView) withObject:nil];
And:
-(void) populateScrollView{
...creating scrollView...
[self.view addSubview:scrollView];
[spinner removeFromSuperview];
}
It works perfectly. What is really weird for me, is that I am adding the scrollview to the UI in a background thread, not in the main thread. I thought I could only mess with the UI in the main thread.
(I could do the same thing with GCD. When the background thread finished loading the scrollview, I could display scrollview in the main thread's queue. But the former solution somehow works as well...)

Sorry for ugly text formatting.
There is a very cute way to do what you want.
Firstly you have to show the indicator view and only after a little amount of time (0.1 sec or even lesser) you ask your scrollView to populate.
- (void)loadView {
CGRect fullScreenRect=[[UIScreen mainScreen] bounds];
UIView *contentView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 340, fullScreenRect.size.width)]autorelease];
self.view = contentView;
spinner = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
spinner.center = self.view.center;
[self.view addSubview:spinner];
[spinner startAnimating];
double delayInSeconds = 0.1;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
scrollView=[[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 340, fullScreenRect.size.width)]autorelease];
...setting up scrollView...
[self.view addSubview:scrollView];
});
}

Try this simple method, Its working well for me....
UIActivityIndicatorView *activityIndicator= [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
activityIndicator.layer.cornerRadius = 05;
activityIndicator.opaque = NO;
activityIndicator.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.6f];
activityIndicator.center = self.view.center;
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[activityIndicator setColor:[UIColor colorWithRed:0.6 green:0.8 blue:1.0 alpha:1.0]];
[self.view addSubview: activityIndicator];
Add following lines while load your view
//-- Add this line while processing to load your view
[activityIndicator startAnimating];
//-- Add this line when after you view loaded
[activityIndicator stopAnimating];

Related

UIWindow addSubview handle events

I want to build a custom alert by UIViewController and add its view as a subview to the current window. The view displays very well, but I can't handle any touch events.
I have tried many times to add a button to this viewcontroller and add a target. I have also tried adding a UITapGestureRecognizer and set view's userInteractionEnabled to true, but this also failed even when I cleared all subviews just left the button.
Have I missed something?
Here is the code:
CustomAlert Viewcontroller:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor clearColor];
backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
backgroundView.backgroundColor = [UIColor blackColor];
backgroundView.alpha = 0.5;
[self.view addSubview:backgroundView];
backImage = [[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 300) / 2, (self.view.frame.size.height - 250) / 2, 300, 250)];
backImage.image = [UIImage imageNamed:#"AlertBackground"];
[self.view addSubview:backImage];
confirmButton = [[UIButton alloc]initWithFrame:CGRectMake( backImage.frame.origin.x + 100 , backImage.frame.origin.y + 250 - 40, 100, 26)];
[self.view addSubview:confirmButton];
[confirmButton addTarget:self action:#selector(confirmButtonClick:) forControlEvents:UIControlEventTouchUpInside];
confirmButton.backgroundColor = [UIColor redColor];
[confirmButton setTitle:#"click me" forState:UIControlStateNormal];
}
-(void)confirmButtonClick:(UIButton*)sender{
[self.view removeFromSuperview];
}
-(void)show{
UIWindow * window = (UIWindow*)[UIApplication sharedApplication].keyWindow;
[window addSubview:self.view];
}
Method call custom alert:
CustomAlert * alert = [[CustomAlert alloc]init];
[alert show];
Try to rewrite your -show{} method with these
UIWindow * window = (UIWindow*)[UIApplication sharedApplication].keyWindow;
[window.rootViewController.view addSubview:self.view];
UPDATE:
And if previous don't help, try to overwrite
-(void)show{
UIWindow * window = (UIWindow*)[UIApplication sharedApplication].keyWindow;
[window.rootViewController addChildViewController:self];
[window.rootViewController.view addSubview:self.view];
[self didMoveToParentViewController:window.rootViewController];
}
Those three methods establish parent-child relations.

UIActivityIndicator doesn't stop in iOS

I am showing spinner(UIActivityIndicator) while download is going on. It shows spinner as download starts, but it doesn't stop showing once download is finished. i have seen some question for the same issue but did not help. I need to make it work in ios7 as well as iOS8.
I am using below code...
- (void) startSpinner:(NSString *)message {
container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
container.center = self.view.center;
container.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.4];
container.layer.cornerRadius = 10;
container.layer.masksToBounds = true;
container.tag = 134;
activityLabel = [[UILabel alloc] init];
activityLabel.frame = CGRectMake(0, 70, 100, 30);
activityLabel.text = message;
activityLabel.textColor = [UIColor darkGrayColor];
activityLabel.textAlignment = NSTextAlignmentCenter;
activityLabel.font = [UIFont boldSystemFontOfSize:10];
[container addSubview:activityLabel];
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center = CGPointMake(container.frame.size.width/2, container.frame.size.height/2);
[container addSubview:activityIndicator];
[self.view addSubview:container];
[activityIndicator startAnimating];
}
-(void)stopSpinner{
[activityIndicator stopAnimating];
container = [self.view viewWithTag:134];
[container removeFromSuperview];
activityIndicator = nil;
NSLog(#"activity indicator should be removed %#",activityIndicator);
}
And one more issue it does not show spinner in the center of the device screen.
Any help will be appreciated....
There are a few things wrong here.
container.center = self.view.center;
If you are trying to center the container in self.view, then this won't do it. self.view.center is the center of the view's frame but you would need to use the center of the view's bounds. Here is the corrected code:
container.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
You also need to change how you set the activity indicator's frame:
activityIndicator.center = CGPointMake(CGRectGetMidX(container.bounds), CGRectGetMidY(container.bounds));
Also, if you call startSpinner more than once, you can have more than one spinner added to the view, which could possibly be the reason your spinner doesn't seem to be stopping. I recommend that you alloc and init only one spinner and do it in the class constructor. You can do the same with the container and activityLabel objects. Alternatively, you could call stopSpinner at the top of startSpinner (and add some null-checks in stopSpinner to avoid referencing a null pointer).
It's impossible to say for sure based on your code why it appears that the spinner doesn't stop, but my guess is that you're calling startSpinner several times in a row without any intervening call to stopSpinner.
If you're only calling stopSpinner once then verify that you're doing so in the main thread.
You can add the spinner view on navigation controller, so it will look at the center as you want. I think this will solve your problem.
[self.navigationController.view addSubview:coverView];
And don't forget to remove it from self.navigationController.view

How to align view and subview center Objective-C

I have these two views that i would like to align to be centered.
self.loadingView = [[UIView alloc] initWithFrame:CGRectMake(75, 155, 120, 120)];
self.loadingView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
self.loadingView.clipsToBounds = YES;
self.loadingView.layer.cornerRadius = 10.0;
self.activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
self.activityView.frame = CGRectMake(65, 40, self.activityView.bounds.size.width, self.activityView.bounds.size.height);
[self.loadingView addSubview:self.activityView];
loadingView needs to be aligned center to the iPhones screen. This means a code that can support different sizes of screens. And then activityView should be aligned centered inside the loadingView.
I have tried some code examples but none worked. And it was really confusing. If someone could provide some code exampels and explaing a bit how it works to get a hang of this.
EDIT1
I wrote just as you did i added the self.view.center part but it is still not in the middle?
Also the second question was how to add the indicator view in the CENTER of the loadingView.
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor= [UIColor blackColor];
UIActivityIndicatorView * indicator;
indicator = [[UIActivityIndicatorView alloc] init];
indicator.center = self.view.center;
indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[indicator startAnimating];
[self.view addSubview:indicator];
// Do any additional setup after loading the view, typically from a nib.
}
this will add the activity indicator in the center of the screen..

UILabel not beeing displaying instantly using thread

I want to load some views in thread to avoid UI freeze waiting loading end.
I'm not used to thread so I make a quick test. My code just try to create views in a thread and add this views in the current viewcontroller view in the main thread.
My UIView is working, but for my UILabel I have to wait between 20-60s to have it on the screen.
I make a test with a UIButton and in this case the button display instantly but the label inside the button display with the same delay than my UILabel.
The only way to have it working as I want is to add a [lbl setNeedsDisplay]; in the main thread to force the UILabel to be displayed instantly. Why? Is it possible to do the job without this line?
dispatch_queue_t queue = dispatch_queue_create("myqueue", NULL);
dispatch_async(queue, ^{
// NEW THREAD
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 48)];
lbl.text = #"FOO";
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
view.backgroundColor = [UIColor redColor];
// MAIN THREAD
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:lbl];
[lbl setNeedsDisplay]; // Needeed to see the UILabel. WHY???
[self.view addSubview:view];
});
});
dispatch_release(queue);
You should set the text of the label on the main queue as well:
dispatch_async( dispatch_get_main_queue(), ^{
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 48)]
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
lbl.text = #"FOO";
[self.view addSubview:lbl];
[self.view addSubview:view];
});
It best to keep all UI stuff on the main queue.
UPDATE:
It appears initWithFrame: is not thread safe (found on the SO answer, see also Threading Considerations in the UIView docs).

creating uiview programmatically?

Creating a view with following code.
- (void)loadView {
paintView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
[paintView setBackgroundColor:[UIColor yellowColor]];
self.view = paintView;
[paintView release];
But my problem is whole screen fills with yellow color, but I want with specified frame.
I am creating this view in a view based application.. Am i doing something wrong, Overridind the original view?
You can do it in following way.
- (void)loadView {
/// make a empty view to self.view
/// after calling [super loadView], self.view won't be nil anymore.
[super loadView];
paintView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
[paintView setBackgroundColor:[UIColor yellowColor]];
[self.view addSubview:paintView];
[paintView release];
};
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,35)];
newView.backgroundColor=[UIColor clearColor];
UITextView *mytext = [[UITextView alloc] initWithFrame:CGRectMake(5.0, 0.0, 100.0, 28.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.font = [UIFont systemFontOfSize:15];
mytext.text = #"Mytext";
mytext.scrollEnabled=NO;
[mytext release];
[newView addSubview:mytext];
[self.view addSubview:newView];
replace self.view =paintView;
by
[self.view addSubview: paintView];
I am Going change Line No 3 in LoadView Method , you should add the subView in main View instead on assiging it Directly.
[self.view addSubview:paintview];

Resources