我已经创建了一个UIView的简单子类,它有一些子视图,但是它因为我看不到的原因而破坏。
在我的故事板视图控制器中,我添加了一个UIView,并将其设置为我想要的形状,并将自定义类添加到其中。我的子类的内容如下:
@implementation PersonDetailCell
@synthesize button, requiredMarker, textField;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)layoutSubviews
{
[self setBackgroundColor:[UIColor colorWithRed:87.0f/255.0f green:153.0f/255.0f blue:191.0f/255.0f alpha:1.0f]];
textField = [[DetailTextField alloc] initWithFrame:CGRectMake(10, 0, self.frame.size.width -20, self.frame.size.height)];
[textField setFont:[UIFont fontWithName:@"Helvetica" size:14.0f]];
[textField setTextColor:[UIColor whiteColor]];
[textField setHidden:YES];
[self addSubview:textField];
button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setHidden:YES];
[self addSubview:button];
requiredMarker = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 4, 22)];
[requiredMarker setBackgroundColor:[UIColor redColor]];
[requiredMarker setHidden:YES];
[self addSubview:requiredMarker];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
然后在我的视图控制器代码中,我导入了我的子类,并在我的故事板中将它的IBOutlet连接到我的视图中。在我的视图控制器的viewDidLoad中,我也尝试设置此视图的背景颜色,但没有任何反应。
我的所有代码都运行没有崩溃,但我的问题是:
我无法从视图控制器(或任何其他属性)设置背景颜色。
当我运行应用程序时,高度超过我在故事板中设置的高度(宽度很好),但我无处更改视图的高度。
有什么想法吗?我是否以某种方式使用了错误的方法?
感谢。
答案 0 :(得分:1)
如果是表格单元格,则该类应该扩展UITableViewCell。可以在故事板上添加按钮和文本字段 - 然后您可以使用struts和弹簧(Size Inspector窗格)来确保视图正确调整大小。我不认为layoutSubviews应该用于添加新的UI元素(如果需要手动调整,可能需要调整大小)。
答案 1 :(得分:0)
以下是一些有效的代码:
@interface ChildView()
@property (strong, nonatomic) UITextField *textField;
@property (strong, nonatomic) UIButton *button;
@property (strong, nonatomic) UIImageView *requiredMarker;
@end
@implementation ChildView
@synthesize textField= _textField;
@synthesize button = _button;
@synthesize requiredMarker = _requiredMarker;
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self setBackgroundColor:[UIColor colorWithRed:87.0f/255.0f green:153.0f/255.0f blue:191.0f/255.0f alpha:1.0f]];
self.textField = [[UITextField alloc] init];
[self.textField setFont:[UIFont fontWithName:@"Helvetica" size:14.0f]];
[self.textField setTextColor:[UIColor whiteColor]];
self.textField.text = @"Test text";
[self addSubview:self.textField];
self.button = [[UIButton alloc] init];
self.button.backgroundColor = [UIColor greenColor];
[self addSubview:self.button];
self.requiredMarker = [[UIImageView alloc] init];
[self.requiredMarker setBackgroundColor:[UIColor redColor]];
[self addSubview:self.requiredMarker];
}
return self;
}
-(void)layoutSubviews
{
CGSize hostSize = self.frame.size;
float length = 22;
self.textField.frame = CGRectMake(10, length, hostSize.width, hostSize.height-length);
self.button.frame = CGRectMake(0, 0, hostSize.width, length);
self.requiredMarker.frame = CGRectMake(0, 0, 4, length);
}
@end