Я не уверен, что добавление UIProgressView
в качестве подпредставления объекта UITextField
будет полезно, так как вы не можете изменить рамку просмотра прогресса.
Подклассификация кажется правильным подходом.Вот что я мог придумать.Проверьте, полезно ли это вам.
ProgressField.h
@interface ProgressField : UITextField {
}
@property (nonatomic, assign) CGFloat progress;
@property (nonatomic, retain) UIColor * progressColor;
@end
ProgressField.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
с borderStyle
, установленным на UITextBorderStyleRoundedRect
.