【源码】iOS自定义进度条高度(UIProgressView进度条高度改变的实现)

今天自定义了iOS中的进度条,发现系统的进度条高度无法改变,

现在自己封装了一种进度条(实际是是UIView而不是UIProgressView),可以改变进度条的高度,非常好用,分享给大家,直接上代码:

//  CTWBProgress.h

//  Created by WBapple on 16/7/31.

//  Copyright © 2016年 王彬. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@interface CTWBProgress : UIView

// 进度条背景图片

@property (retain, nonatomic) UIImageView *trackView;

// 进图条填充图片

@property (retain, nonatomic) UIImageView *progressView;

//进度

@property (nonatomic) CGFloat targetProgress;

//设置进度条的值

- (void)setProgress:(CGFloat)progress;

@end

 

 

 

//  CTWBProgresss.m

//  Created by WBapple on 16/7/31.

//  Copyright © 2016年 王彬. All rights reserved.

#import "CTWBProgress.h"

 

@implementation CTWBProgress

//初始化进度条(此处设置进度条背景图片和进图条填充图片)

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self)

    {

        self.backgroundColor = [UIColor clearColor];

        // 背景图像

        _trackView =

        [[UIImageView alloc] initWithFrame:CGRectMake (0, 0, frame.size.width, frame.size.height)];

        [_trackView setImage:[UIImage imageNamed:@"1.png"]];

        //当前view的主要作用是将出界了的_progressView剪切掉,所以需将clipsToBounds设置为YES

        _trackView.clipsToBounds = YES;

        [self addSubview:_trackView];

        // 填充图像

        _progressView = [[UIImageView alloc]

        initWithFrame:CGRectMake (0 - frame.size.width, 0, frame.size.width, frame.size.height)];

        [_progressView setImage:[UIImage imageNamed:@"2.png"]];

        [_trackView addSubview:_progressView];

    }

    return self;

}

//设置进度条的值

- (void)setProgress:(CGFloat)progress

{

    _targetProgress = progress;

    [self changeProgressViewFrame];

}

//修改显示内容

- (void)changeProgressViewFrame

{

    _progressView.frame = CGRectMake (self.frame.size.width * _targetProgress - self.frame.size.width,

                                      0, self.frame.size.width, self.frame.size.height);

}

 

@end

 

使用进度条方法

#import "CTWBProgress.h"

 CTWBProgress* progressView;

  float progressVlaue;

   // 给进度条一个frame,给进度条设置值即可

     progressView = [[CTWBProgress alloc] initWithFrame:CGRectMake(FITWIDTH(108), FITHEIGHT(960), FITWIDTH(864), FITHEIGHT(80))];

   [progressView setProgress:progressVlaue];

    [self.view addSubview:progressView];

 

posted @ 2016-07-31 01:29  王彬iOS  阅读(6425)  评论(0编辑  收藏  举报