我创建了一个简单的 UIView 子类,它有一些 subview ,但由于我看不到的原因而损坏。
在我的 Storyboard View Controller 中,我添加了一个 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
然后在我的 View Controller 代码中,我导入了我的子类并将其 IBOutlet 连接到 Storyboard中的 View 。在 View Controller 的 viewDidLoad 中,我还尝试设置该 View 的背景颜色,但没有任何反应。
我的所有代码都运行时没有崩溃,但我的问题:
我无法从 View Controller 设置背景颜色(或任何其他相关属性)。
当我运行应用程序时,高度大于我在 Storyboard 中设置的高度(宽度很好),但我无法更改 View 的高度。
有什么想法吗?我是否使用了错误的方法?
谢谢。
最佳答案
如果它是一个表格单元格,那么该类应该扩展 UITableViewCell。可以在 Storyboard上添加按钮和文本字段 - 然后您可以使用支柱和 Spring (大小检查器 Pane )来确保 View 正确调整大小。我不认为layoutSubviews 应该用于添加新的UI 元素(如果您需要手动执行此操作,则可能用于调整大小)。
关于iphone - 简单的自定义 UIView 类已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13280463/