在UITextField对象内部显示进度条

| 如何在ѭ0内绘制进度条?到目前为止,我已经测试了两种方法。 1.添加一个“ 1”对象作为“ 0”对象的子视图。
UIProgressView* progressView = [[UIProgressView alloc] init];
[aUITextField addSubview:progressView];
progressView.progress = 0.5;
[progressView release];
2.子类
UITextfield
,并覆盖
drawRect:
- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
        [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    [[UIColor orangeColor] setFill];
    [[UIBezierPath bezierPathWithOvalInRect:rect] fill];
}
两种方法均无效。您认为这些方法有什么问题吗?我该如何做呢?     
已邀请:
        我不确定将“ 1”添加为“ 0”对象的子视图是否有用,因为您无法更改进度视图的框架。 子类化似乎是正确的方法。这是我能想到的。检查它是否对您有用。 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;
}
这似乎不适用于将
borderStyle
设置为
UITextBorderStyleRoundedRect
UITextField
。     
        
UIProgressView* progressView = [[UIProgressView alloc] init];
progressView.frame = aUITextField.frame;// you can give even set the frame of your own using CGRectMake();
[aUITextField addSubview:progressView];
progressView.progress = 0.5;
[progressView release];
设置进度视图的框架。     
        在这里,我认为您必须将progressView作为子视图添加到self.view,只需根据适合UITextField的大小设置progressView的框架,然后将progressview的中心设置为UITextField的中心。  希望对您有帮助。     

要回复问题请先登录注册