【源码】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];
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库