Search code examples
objective-cscrollscrollviewprogressuiprogressview

Specified the scroll of a UIScrollView with an UIProgressView


I would like to add a progress bar under the navigation bar which will indicate the progress of the scroll of a UIScrollView, I use (not work) : I do

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
self.progressView.progress = scrollView.contentOffset.y;
}

Solution

  • check now I created one demo:

           #import "ViewController.h"
    
        @interface ViewController ()<UIScrollViewDelegate>{
            UIScrollView *scroll;
            UIProgressView *indicater;
            }
        @end
    
        @implementation ViewController
    
    
    
        - (void)viewDidLoad {
            indicater=[[UIProgressView alloc]initWithFrame:CGRectMake(0, 65, 320,10)];
            indicater.backgroundColor=[UIColor redColor];
            indicater.progress = 0.0;
            indicater.hidden=false;
    
    
            [self.view addSubview:indicater];
    
            scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(10, 80,300, 200)];
            scroll.backgroundColor=[UIColor greenColor];
            scroll.userInteractionEnabled=YES;
            scroll.delegate=self;
            scroll.contentSize = CGSizeMake(300 ,5000);
    
            [self.view addSubview:scroll];
    
    
    
        }
        - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
            CGPoint offset = scroll.contentOffset;
            CGRect bounds = scroll.bounds;
            CGSize size = scroll.contentSize;
            UIEdgeInsets inset = scroll.contentInset;
            float y = offset.y + bounds.size.height - inset.bottom;
            float h = size.height;
            NSLog(@"%f",y);
            NSLog(@"%f",h);
            NSNumber *num=[NSNumber numberWithFloat:y];
            NSNumber *num1=[NSNumber numberWithFloat:h];
    
    
    
        [self UpdateProgressbar:num TotalscrollLenrth:num1];
    
    }
    
    -(void)UpdateProgressbar:(NSNumber*)currentscrollLenrth TotalscrollLenrth:(NSNumber*)n
    {
    
        NSString *totalLength = [NSString stringWithFormat:@"%@", n];
        NSLog(@"%@", totalLength);
    
        NSString *currentScrool = [NSString stringWithFormat:@"%@", currentscrollLenrth];
        NSLog(@"%@", currentScrool);
        if (currentscrollLenrth <= n) {
            [indicater setProgress:([currentScrool intValue]/[totalLength floatValue])];
        }
        else {
                      // do somithig
    
        }
    }