简体   繁体   中英

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.

ie

[self.subView addSubView: activityIndicator1];

activityIndicator1's center should be your subview's center.

activityIndicator1.center = subView.center;

Hope this helps.

Thanks

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.

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;

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM