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)来显示进度,但是不遮住导航栏上的标题和按钮等????
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 我们要实现上面图片的效果,在滑动条滑动改变的同时,进度条也跟随着改变。 在 ViewController.h里面声...
    来敲代码阅读 4,086评论 0 0
  • 简介: 三姐妹安雨萱,安雨潇,杨晓,出生在小城镇,却立志要到大城市打拼,希望能拥有属于自己的一片天地,在爱情,工作...
    琉璃花楹阅读 3,660评论 17 21
  • 她怔怔的盯着那双被擦得锃亮的黑色高级皮靴。 皮靴在耀眼的橙黄色灯光下更加闪耀。 她不知道那是什么牌子,只知道那是她...
    苏小眠233阅读 2,206评论 0 1
  • 一双忧郁的眼睛 温暖如烛光 照亮黑暗的阴影 暗香浮动情意绵绵 那是一场青春逃不掉的劫 相爱却分离着苦痛 眉宇里写满...
    玉妮阅读 2,166评论 2 4
  • 不记得哪儿看到的这句话:笑容腼腆的孩子,都很漂亮。当初看到这句话的时候,我想到了我的男朋友,但工作以来,又看到了两...
    小懒神阅读 3,122评论 0 0

友情链接更多精彩内容