#iphone #cocoa-touch #uitextfield #uiprogressview
#iPhone #cocoa-touch #uitextfield #uiprogressview
Вопрос:
Как можно нарисовать индикатор выполнения внутри UITextField
? До сих пор я тестировал два способа.
1. Добавьте UIProgressView
объект в качестве подвида UITextField
объекта.
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:
Я не уверен, что добавление 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
s с borderStyle
значением UITextBorderStyleRoundedRect
.
Комментарии:
1. Я пытаюсь использовать ваши коды, но по-прежнему нет желаемого результата. Я сообщу о результате, если получу правильное отображение
2. Конечно, вы можете посмотреть на
this sample
. Используйте ползунок для имитации изменения вprogress
.3. Большое спасибо ааааааааааааааааааааааааааааааааааааааааааааа
Ответ №2:
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.
Ответ №3:
Здесь, я думаю, вам нужно добавить progressView в качестве подпросмотра в self.view , просто установите рамку progressView в соответствии с размером, который будет соответствовать UITextField , и установите центр progressview в центр UITextField . надеюсь, это поможет вам.