Display activity indicator only after clicking the button - ios

hi I’m trying view the activity indicator by clicking the button. I have tried but i having some issues. If i drag the activity indicator to my storyboard but its visible while running my app i want to display while clicking the button. in my app I’m giving the photo upload option. So if they click the upload button i want to view the activity indicator . ..
this is code i have tired but its not working.
this is my h file where i have implemented the activity indicator and the button..
- (IBAction)click:(id)sender;
#property (strong, nonatomic) IBOutlet UIActivityIndicatorView *spinner;
this is code which i have used in my m file...
-(void)temp
{
spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
[spinner setCenter:CGPointMake(480/2.0, 128.0/2)]; // (mid of screen) I do this because I'm in landscape mode
[self.view addSubview:spinner];
[spinner startAnimating];
[spinner release];
}
and im calling this function in my button..
- (IBAction)click:(id)sender {
[self temp];
}
and also im have some other issues i saw the code here the problem is
[yourView addSubview:spinner];
in yourview i give my uiviewcontroller name but its giving error i have changed to
[self.view addSubview:spinner];
pls tell me where im doing wrong and what is the right way to make it.. thanks

Man I got the problem...I think you are adding white activity indicator on white view that's why it is not being display but it is there for sure so...use some other style UIActivityIndicatorViewStyleGray
-(void)temp
{
spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
[spinner setCenter:CGPointMake(480/2.0, 128.0/2)]; // (mid of screen) I do this because I'm in landscape mode
[self.view addSubview:spinner];
[spinner startAnimating];
[spinner release];
}

First of all since you already have an IBOutlet for the spinner so do not allocate a new one in temp method. Modify your temp method to look like this:
-(void)temp
{
spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
[spinner setCenter:CGPointMake(480/2.0, 128.0/2)]; // (mid of screen) I do this because I'm in landscape mode
[self.view addSubview:spinner];
[spinner startAnimating];
}

I f you are using interface builder like Storyboard or XIB then just set the frame and evrything there itself. And if you want to change the property of it at runtime just use the IBOutlet you have used.
You need not to instantiate again just use its getter and setter to use it accordingly. So here you can do.
- (IBAction)click:(id)sender
{
[self.spinner startAnimating];
}
and synthesize spinner before using it.
And if you want to use it programmatically delete it from XIB and remove all references corresponding to it. And declare in .h file
UIActivityIndicatorView *spinner;
or you can make a property
#property(nonatomic, strong)UIActivityIndicatorView *spinner;
and just use your own code.

Assuming your spinner connected to your view.
In your viewDidLoad you can setup your view, then it'll show when you tap the button:
- (void)viewDidLoad
{
[super viewDidLoad];
self.spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
[self.spinner setCenter:CGPointMake(480/2.0, 128.0/2)]; // (mid of screen) I do this because I'm in landscape mode
self.spinner.hidden = YES;
}
- (IBAction)click:(id)sender
{
self.spinner.hidden = NO;
[self.spinner startAnimating];
// Additionally you can show spinner in top bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)stopSpinner
{
if (self.spinner.isAnimating) {
self.spinner.hidden = YES;
[self.spinner stopAnimating];
// And hide it when you don't need it anymore
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
}
I suggest you see this answer: How to get orientation-dependent height and width of the screen? to re position your spinner.

Related

iOS activity indicator issue

I am creating a UITableview inside a subview. For populating table I am using some jsonget request. During the process I want to show an activity indicator. I am not getting why it's not showing.
Sample code:
activityIndicator1 = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityIndicator1.center = CGPointMake([[UIScreen mainScreen]bounds].size.width/2, [[UIScreen mainScreen]bounds].size.height/3);
[self.window addSubview:activityIndicator1];
activityIndicator1.color=[UIColor greenColor];
[self bringSubviewToFront:activityIndicator1];
[UIApplication sharedApplication].networkActivityIndicatorVisible = TRUE;
[activityIndicator1 startAnimating];
Problem is activity indicator not showing.
Thanks and Regards
Vikas
You need to add activityIndicator1 in your subvview.
i.e.
[self.subView addSubView: activityIndicator1];
activityIndicator1's center should be your subview's center.
activityIndicator1.center = subView.center;
Hope this helps.
Thanks
UIActivityIndicator's hidesWhenStopped property is YES by default. Also, it's stopped by default. So your activity indicator is just hidden. Add this code:
activityIndicator1.hidden = NO;
do this instead
UIView* WaitingView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
WaitingView.backgroundColor=[UIColor blackColor];
WaitingView.alpha=0.5;
UIActivityIndicatorView* activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:
UIActivityIndicatorViewStyleWhiteLarge ];
[activityView setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)];
[activityView setContentMode:UIViewContentModeCenter];
[WaitingView addSubview:activityView];
[self.view addSubview:WaitingView];
[activityView startAnimating];
When you're refreshing a table with fresh data from an api request, a common and handy tool is UIRefreshControl, which will show as an ActivityIndicator spinning, when the user pulls the table to refresh, while making the call to pull the fresh data.
#property (strong, nonatomic) UIRefreshControl *refreshControl;
then within your viewDidLoad create the refreshControl and add the target action
-(void)viewDidLoad{
[super viewDidLoad]
self.refreshControl = [[UIRefreshControl alloc]init];
[self.refreshControl addTarget:self action:#selector(refreshTable) forControlEvents:UIControlEventValueChanged];
[yourTableView addSubview:self.refreshControl];
}
By pulling the table in a downward motion, the activity indicator will show and the call to refresh the table will be made.
-(void)refreshTable{
// Here your call to pull data from your API to refresh the data
// ** Your code here to pull in your JSON data and update your datasource **
//Once the data has been retrieved or failed to retrieve, call this to end the refreshing
[self.refreshControl endRefreshing];
}
if this is what you're trying to recreate, I hope this helps.
Note - user will need to pull the table in a downward 'pull-to-refresh' action to activate this of course
add this in your .h file,
#property(nonatomic, retain) UIActivityIndicatorView *indicator;
than add this code in your button click method,
indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.frame = CGRectMake(0.0, 0.0, 60.0, 60.0);
indicator.color = [UIColor colorWithRed:0.949 green:0.571 blue:0.558 alpha:1.000];
[indicator setBackgroundColor:[UIColor colorWithRed:1 green:1.000 blue:1 alpha:1.000]];
indicator.center = self.view.center;
[self.view addSubview:indicator];
[indicator bringSubviewToFront:self.view];
[UIApplication sharedApplication].networkActivityIndicatorVisible = TRUE;
[indicator startAnimating];
than stop indicator where you want using,
[indicator stopAnimating];
hope this will help you.

How to create activity indicator for collection view .see detail in post

i have one collection view. i tried with storyboard but not able to add. Now how to create a uiactivity indicator with some 2.0 sec delay.and that should be in middle centre of my collection view using programatically
Any can help me.I am new to ios. i have tried but no animation to see.Please help me out.
need activity indicator at middle when my view loads
should start animating for 2.0 sec, after that its should hide.Whenever my collection view screen shows up that indicator should start animating for 2.0 sec and should hide
i have tried this code:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//self.navigationItem.hidesBackButton = YES;
programaticActivityIndicatorView_ = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[self.programaticActivityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.programaticActivityIndicatorView setHidesWhenStopped:YES];
[self.programaticActivityIndicatorView setCenter:CGPointMake(150, 239)];
[self performSelector:#selector(methodsq) withObject:nil afterDelay:3.0];
[self.view addSubview:self.programaticActivityIndicatorView];
}
-(void)methodsq {
[programaticActivityIndicatorView_ startAnimating];
}
But i continuously animating , but i need to animate only for 3.0 sec alone after that it should hide
Thanks !
Declare Property
#property (strong, nonatomic) UIActivityIndicatorView *indicator;
Synthesize it
#synthesize indicator;
Create Process
CGRect rect = [[UIScreen mainScreen] bounds];
self.indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.indicator.frame = CGRectMake((rect.size.width-50)/2, (rect.size.height-50)/2, 50, 50);
self.indicator.hidesWhenStopped = YES;
[self.view addSubview:self.indicator];
Start process
[self.indicator performSelector:#selector(startAnimating) withObject:nil afterDelay:0.1];
Hide when you want
[self.indicator performSelector:#selector(stopAnimating) withObject:nil afterDelay:2.0];

UIActivityIndicator Disappear from View When app goes to the background and when switching tabs

I have an issue which is not a big deal but driving me crazy.
I have a view which subclasses UITableView.
Pressing on one of the cells leader to an lengthy HTTP requests, which I run on another thread using this code in didSelectRowAtIndexPath:
case SECTION_SEND:
[self addActivityIndicatorToView];
[self performSelectorInBackground:#selector(sendEmail) withObject:nil];
break;
The activityIndicator is behaving correctly. Displaying on the screen and then removing itself when the operation ends. But if the app goes to the background or another tab, the activity indicator disappears, even though the task did not finish.
Here are the methods I am using:
Then:
-(UIActivityIndicatorView*)createActivityIndicator {
//Setting up activity indicator
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.color = [UIColor whiteColor];
spinner.backgroundColor = [UIColor blackColor];
spinner.alpha = 0.8;
CGFloat width = 120;
spinner.frame = CGRectMake(self.tableView.bounds.size.width/2-width/2, self.tableView.bounds.size.height/2-width/2, width, width);
//spinner.center = CGPointMake( [UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);
spinner.layer.cornerRadius = 20;
spinner.layer.masksToBounds = YES;
return spinner;
}
-(void)addActivityIndicatorToView {
//[[[[UIApplication sharedApplication] delegate] window] addSubview:ai];
_ai = [self createActivityIndicator];
[self.tableView.superview addSubview:_ai];
[_ai startAnimating];
}
-(void) removeActivityIndicatorFromView {
[_ai stopAnimating];
[_ai removeFromSuperview];
}
I am calling [self removeActivityIndicatorFromView] when the operation ends.
As I said, it behaves properly unless I leave the screen. I have check my viewdidload and viewwilldisappear viewwillappear and init methods and I have nothing overriding _ai.
Any ideas?

How to set activity indicator for a view when Click the table cell

I am developing one iPad application using story board.In my view controller one table is set as the left side and one more view is set as the right side.I need to set one activity controller above the right side view.Which need to start animating when i click the right side table cell and need to stop after 1s.I am using 'didSelectRowAtIndexPath' function for select table cell.How i will set activity indicator for my situation.
In didSelectRowAtIndexPath method you could make a call to below function:-
-(void)setActivityIndcator:(UIView *)view
{
transparentView = [[UIView alloc] init];
transparentView.frame = view.frame; //Provide frame of right side view.
transparentView.alpha = 0.5; //To provide transparent look and feel.
activityInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityInd.frame = CGRectMake(transparentView.frame.size.width/2, transparentView.frame.size.height/2, 50, 50); //Change as you want it to be
[transparentView addSubview:activityInd];
[activityInd startAnimating];
[self performSelector:#selector(stopIndicator) withObject:self afterDelay:1];
}
-(void)stopIndicator //This method will be called after delay and would remove view and activityInd
{
[activityInd stopAnimating];
[activityInd removeFromSuperview];
[transparentView removeFromSuperview];
}
Also you could create a subclass of it and use it in any viewcontroller by creating it's object. That why u don't need to write down all function everytime.
use this in this method
didSelectRowAtIndexPath
UIActivityIndicatorView *activityView =
[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityView.frame = CGRectMake(120, 230, 50, 50);
[self.view addSubview:activityView];
Use the delegate method didSelectRowAtIndexPath of TableView and write the following code into it
UIActivityIndicatorView *activity =
[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activity.frame = CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/2, 50, 50);
[self.view addSubview:activity];
[activity startAnimating];
Stope this when your work is done and remove it from the view

How to insert a UIActivityIndicator into search bar textfield?

I want to add a UIActivityIndicator in the right side of textfield of the search bar.
So that search bar will keep on showing loading status while it fetches data from DB.
Any idea about this?
This is away to change the search magnifier with the spinner
UITextField *searchField = nil;
for (UIView *subview in searchBar.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)subview;
break;
}
}
if (searchField) {
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
searchField.leftView = spinner;
}
or if you like you can add the spinner over the search bar views
[self.searchBar addSubview:spinner];
Add the activity indicator as the subView of searchbar like :
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.center = CGPointMake(160, 240);
spinner.hidesWhenStopped = YES;
spinner.tag = 7;
[self.searchBar addSubview:spinner];
}
And when you want to remove the activity indicator you can use the tag.
Adding my comment as answer.
You can try to use [searchbar addSubview:activityIndicator]; There are no in built ways to do this. Only thing you can change is the search icon using the method setImage:forSearchBarIcon:state:. This can be used to add a different image in searchbar. Apple doc on Searchbar. A work around would be to loop through the subviews of the search bar and add the activity indicator to the subview at required place.

Resources