ios - UILabel 未显示在自定义 UIView 屏幕中

标签 ios objective-c iphone uiview uilabel

这是我实现自定义 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/

相关文章:

ios - 在 iOS 中从 RSS Feed 获取图像

iphone - 访问 UIWebView 的 subview 是否违反 Apple 3.3.1 协议(protocol)

ios - 从数组加载数据时遇到问题

ios - 子类 UIView 错误

ios - 列出 Podfile 中特定依赖项使用的所有依赖项

Objective-C 和组件编程

iPhone - UIWebView 多重锁定

iphone - 如何在 iOS 上的应用程序之间共享值

ios - liberty lite 越狱检测失败

ios - 如何快速增加 ScrollView 中的 UITableView 高度