UIActivityIndicator doesn't stop in iOS - 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

Related

How do I add an activity indicator view to my UITableViewController programmatically?

I have a UITableViewController that I need to add an activity indicator view to. I want the activity indicator to animate until the tableview has loaded. I've tried [self.view addSubview:self.activityIndicator]; but it doesn't appear. What should the activity indicator's parent view be?
I'm not sure which part of this solved my problem, but I think it was giving the activity indicator view a frame that fixed it.
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
activityIndicator.center = self.view.center;
[self.view addSubview:activityIndicator];
[activityIndicator startAnimating];

Activity Indicator is not showing in front of view

I have a view in Which I have added a UIScrollview. In UIScrollView I added an UIImageView. After Picking Image from ImagePicker I am adding image to UIImageView.
Problem : I am showing UIActivityIndicatorView before adding image to ImageView.
Case 1: When I add activity Indicator to self.view It is showing behind the UIScrollView means not showing at all.
Case 2: If I add Indicator to Scrollview it is not adding in view.
I tried many thing bringSubviewToFront , insertSubview:indicator aboveSubview:myscrollview
Here is my CODE :
scroller = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 60, Screen_width, Screen_height - 60)];
scroller.delegate = self;
[self.view addSubview:scroller];
scroller.contentSize = CGSizeMake(Screen_width, 667);
profileImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 10, Screen_width-40, Screen_width-40)];
profileImageView.backgroundColor = gray;
profileImageView.contentMode = UIViewContentModeScaleAspectFill;
profileImageView.clipsToBounds = YES;
profileImageView.userInteractionEnabled=YES;
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
_profileIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
_profileIndicator.frame = CGRectMake(40.0, 20.0, 60.0, 60.0);
_profileIndicator.center = self.view.center;
[self.view addSubview:_profileIndicator];
[_profileIndicator startAnimating];
}
All the UI things should be in main thread. So, please make sure you are using UIActivityIndicator in main thread.

How can i show a activity indicator in table view when i scroll table view to the bottom like in Flipkart in ios?

UITableView always shows an indicator in a footer.
I want to load more items from server when my table view scroll to bottom of the View.
My code is:
UIView *loadMoreV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, homeTV.frame.size.width, 40)];
loadMoreV.backgroundColor = [UIColor clearColor];
loadMoreV.layer.cornerRadius = 5.0f;
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.alpha = 1.0;
activityIndicator.center = CGPointMake(loadMoreV.frame.size.width/2,20);
activityIndicator.hidesWhenStopped = NO;
[loadMoreV addSubview:activityIndicator];
homeTV.tableFooterView = loadMoreV;
[activityIndicator startAnimating];
You need to place a check in CellforRow TableView method to get the last row and fetch data from server in that check.

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..

Displaying UIActivityIndicatorView until UIView is loaded

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];

Resources