我有一个 UITextField 的子类,并且希望能够将它添加到 IB 中的视图中。我所做的是添加 UITextField 并将其类更改为我在 Inspector 的 Identity 选项卡中的子类。但我只能在视图上看到 UITextField 。
代码:
@interface ExtendedTextField : UITextField {
}
//implementation
@implementation ExtendedTextField
- (void)baseInit
{
UIImage * curImage = [UIImage imageNamed:@"tfbg.png"];
[self baseInitWithImage : curImage];
}
- (void)baseInitWithImage : (UIImage *) aImage
{
aImage = [aImage stretchableImageWithLeftCapWidth:29 topCapHeight:29];
self.background = aImage;
self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
UIView * curLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, self.frame.size.height)];
self.leftView = curLeftView;
self.rightView = curLeftView;
[curLeftView release];
self.leftViewMode = UITextFieldViewModeAlways;
self.rightViewMode = UITextFieldViewModeAlways;
[self setTextColor:[UIColor greenColor]];
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self baseInit];
}
/*
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self baseInit];
}
return self;
}
*/
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self)
{
[self baseInit];
}
return self;
}
- (id)initWithFrame:(CGRect)frame withImage:(UIImage*)aImage
{
self = [super initWithFrame:frame];
if (self) {
[self baseInitWithImage : aImage];
}
return self;
}
- (void)dealloc {
[super dealloc];
}
@end
编辑:我注意到几件事:
-- 如果我在里面放 [super initWithFrame:...] 而不是 [super initWithCoder:...]
(id)initWithCoder:(NSCoder *)aDecoder
它运作良好。
-- 现在我使用的是 awakefromnib 而不是 initwithcoder,而在 textfield 中唯一改变的是 textColor。有人可以解释为什么会这样吗?
解决方案我需要将边框样式设置为无。BS 叠加 bg 图像。