我不确定添加UIProgressView
作为UITextField
对象的子视图是否有用,因为您无法更改进度视图的框架。
子类化似乎是正确的方法。这是我能想到的。检查它是否对您有用。
进度字段.h
@interface ProgressField : UITextField {
}
@property (nonatomic, assign) CGFloat progress;
@property (nonatomic, retain) UIColor * progressColor;
@end
进度字段.m
@implementation ProgressField
@synthesize progress;
@synthesize progressColor;
- (void)setProgress:(CGFloat)aProgress {
if ( aProgress < 0.0 || aProgress > 1.0 ) {
return;
}
progress = aProgress;
CGRect progressRect = CGRectZero;
CGSize progressSize = CGSizeMake(progress * CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
progressRect.size = progressSize;
// Create the background image
UIGraphicsBeginImageContext(self.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, self.bounds);
CGContextSetFillColorWithColor(context, [self progressColor].CGColor);
CGContextFillRect(context, progressRect);
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[super setBackground:image];
}
- (void)setBackground:(UIImage *)background {
// NO-OP
}
- (UIImage *)background {
return nil;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self setBorderStyle:UITextBorderStyleBezel];
}
return self;
}
这似乎不适用于设置为UITextField
s 的 s 。borderStyle
UITextBorderStyleRoundedRect