iOS导航栏UINavigationBar添加进度条

  常常看到好多应用push出新界面时,导航栏下方会有一个进度条显示,所以简单的实现了一下这个效果。代码如下:


#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIProgressView *progressView;

@property (nonatomic, strong) NSTimer *longConnectTimer;

@property (nonatomic, assign) int time;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor greenColor];
    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
    self.navigationItem.title = @"导航栏进度条";
    
    
//    // 导航栏底部添加进度条
//    self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 43, CGRectGetWidth(self.view.frame), 40000)];// 高度设置无效,默认是2
    // 状态栏顶部添加进度条
    self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, -20, CGRectGetWidth(self.view.frame), 40000)];// 高度设置无效,默认是2
    self.progressView.backgroundColor = [UIColor clearColor];// 背景颜色,只有trackTintColor为透明色时,才能看到背景颜色
    self.progressView.progressTintColor = [UIColor purpleColor];// 进度部分的颜色
    self.progressView.trackTintColor = [UIColor clearColor];// 未进度部分的颜色
    
//    [self.navigationController.navigationBar addSubview:self.progressView];// 导航栏添加进度条方式一
    [self.navigationController.navigationBar.layer addSublayer:self.progressView.layer];// 导航栏添加进度条方式二


    self.time = 0;
    self.longConnectTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(longConnectToSocket) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.longConnectTimer forMode:NSRunLoopCommonModes];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)longConnectToSocket {
    self.time += 25;
    [self.progressView setProgress:(self.time / 100.0) animated:YES];
    if (100 == self.time) {
        self.time = 0;
    }
}

问题一: 不知道大家有没有更好的实现方式,欢迎留言

问题二:

  • 进度条的高度怎么改变??通过设置transform 属性progressView.transform = CGAffineTransformMakeScale(1.0f, 5.0f);有问题,就有进度条在进度时,一边向有进度,一边向下扩展高度,效果很糟糕。
  • 怎样让进度条铺满整个导航栏的高度(高度64)来显示进度,但是不遮住导航栏上的标题和按钮等????

你可能感兴趣的:(iOS导航栏UINavigationBar添加进度条)