为啥这个自定义 UIView 代码不显示 UILabel 的?
Posted
技术标签:
【中文标题】为啥这个自定义 UIView 代码不显示 UILabel 的?【英文标题】:why does this custom UIView code not display the UILabel's?为什么这个自定义 UIView 代码不显示 UILabel 的? 【发布时间】:2011-03-25 01:44:36 【问题描述】:我有点想知道为什么,当我使用此自定义 UIView 显示一行 3 x UILabels 时,虽然我看到框架的红色轮廓,但在框架内看不到任何东西。那就是标签没有出现,我设置的 UILabel 的黑色边框也没有出现。
任何人都可以在这里发现问题吗?
#import <UIKit/UIKit.h>
@interface LabelRowView : UIView
UILabel *_dateLabel;
UILabel *_titleLabel;
UILabel *_locationLabel;
@property (nonatomic, retain) UILabel *dateLabel;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *locationLabel;
- (void)setFont:(UIFont *)newFont;
- (void)setDataWithDate:(NSString*)dateStr AndTitle:(NSString*)titleStr AndLocation:(NSString*)locationStr;
@end
// ==============================================================
@implementation LabelRowView
@synthesize dateLabel = _dateLabel;
@synthesize titleLabel = _titleLabel;
@synthesize locationLabel = _locationLabel;
- (void)setDefaultsForLabel:(UILabel*)label
label.layer.borderColor = [UIColor blackColor].CGColor;
label.layer.borderWidth = 1.0;
label.textAlignment = UITextAlignmentLeft;
label.lineBreakMode = UILineBreakModeTailTruncation;
label.opaque = YES; // TODO: put back in for performance reasons if possible
label.backgroundColor = [UIColor clearColor];
- (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
if (self)
// Initialization code
self.dateLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, 10)] autorelease]; // Position will be updated later
self.titleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, 10)] autorelease]; // Position will be updated later
self.locationLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, 10)] autorelease]; // Position will be updated later
// Set Defaults
[self setDefaultsForLabel:self.dateLabel];
[self setDefaultsForLabel:self.titleLabel];
[self setDefaultsForLabel:self.locationLabel];
self.layer.borderColor = [UIColor redColor].CGColor;
self.layer.borderWidth = 1.0;
return self;
- (void)setDataWithDate:(NSString*)dateStr AndTitle:(NSString*)titleStr AndLocation:(NSString*)locationStr
self.dateLabel.text = dateStr;
self.titleLabel.text = titleStr;
self.locationLabel.text = locationStr;
- (void)setFont:(UIFont *)newFont
self.dateLabel.font = newFont;
self.titleLabel.font = newFont;
self.locationLabel.font = newFont;
- (void)layoutSubviews
[super layoutSubviews];
// Get current width for this subview
CGFloat contentViewWidth = self.frame.size.width;
CGSize maximumLabelSize = CGSizeMake(contentViewWidth, 9999);
CGFloat oneLineHeight = [@"A" sizeWithFont:self.dateLabel.font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeTailTruncation].height;
// Calculate Widths
CGFloat dateColumnWidth = [@"Ddd 12:22 " sizeWithFont:self.dateLabel.font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeTailTruncation].width;
CGFloat titleColumnWidth = (contentViewWidth - dateColumnWidth) * 2.0/3.0;
CGFloat locationColumnWidth = contentViewWidth - dateColumnWidth - titleColumnWidth;
// Date - Layout
CGRect newRect = CGRectMake(0.0, 0.0, dateColumnWidth, oneLineHeight);
self.dateLabel.frame = newRect;
// Date - Layout
newRect = CGRectMake(dateColumnWidth, 0.0, titleColumnWidth, oneLineHeight);
self.titleLabel.frame = newRect;
// Title - Layout
newRect = CGRectMake(dateColumnWidth + titleColumnWidth, 0.0, locationColumnWidth, oneLineHeight);
self.locationLabel.frame = newRect;
- (void)dealloc
[super dealloc];
@end
【问题讨论】:
【参考方案1】:您似乎没有将 UILabel 添加为子视图。
【讨论】:
doh - 感谢 Arkku,我一直在忽略这一点,因为我一直在查看它 - 检查了从根控制器向下通过层次结构的所有 addSubview,但忘记了叶节点处的那个 :)以上是关于为啥这个自定义 UIView 代码不显示 UILabel 的?的主要内容,如果未能解决你的问题,请参考以下文章