这是我实现自定义 UiView 的方式
在myView.h文件中
@interface myView : UIView
{
NSString *message;
}
...
@property (nonatomic, retain) UILabel *messageLabel;
...
@end
在myView.m文件中
此函数将实例化 myView 并向其添加消息标签
+ (id) initWithText:(NSString *) text
{
screenBounds = [[UIScreen mainScreen] bounds];
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
self = [super initWithFrame:CGRectMake(0, statusBarFrame.size.height, screenBounds.size.width, 40)];
if (self)
{
[self setBackgroundColor:[UIColor redColor]];
message = [text copy];
_messageLabel = [[UILabel alloc]initWithFrame:[self frame]];
[_messageLabel setText:message];
[_messageLabel setAdjustsFontSizeToFitWidth:YES];
[_messageLabel setTextAlignment:NSTextAlignmentJustified];
[_messageLabel setTextColor:[ UIColor blackColor]];
[self addSubview:_messageLabel];
}
return self;
}
稍后我将 myView 作为子类添加到屏幕中的可见 View 。当我运行该应用程序时,我可以看到红色的 myView,但其中未显示消息标签。
最佳答案
当您使用 self.frame 初始化 UILabel 时,您必须考虑其父 View 中的值。
也许您的第二个参数:y = statusBarFrame.size.height 太高了,这就是您的标签不在您视野中的原因?
尝试使用 CGRectMake(0,0,self.frame.width, self.frame.height) 初始化标签
关于ios - UILabel 未显示在自定义 UIView 屏幕中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34201789/