【问题标题】:UILabel is not shown in custom UIView screenUILabel 未显示在自定义 UIView 屏幕中
【发布时间】:2015-12-10 12:21:40
【问题描述】:

这是我实现自定义 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 作为子类添加到屏幕中的可见视图中。当我运行应用程序时,我可以看到红色的 myView,但其中没有显示消息标签。

【问题讨论】:

  • 很可能标签的位置或大小不正确。
  • 你在哪里调用 initWithText?
  • 尝试在 UILabel 初始化中将 [self frame] 更改为 [self bounds]。
  • @SuryaSubenthiran 我试过[self bounds] 即使这样它也没有显示标签。只有红色背景的 UiView 可见
  • @beyowulf 我从另一个文件调用 initWithText。我使用[[[myView alloc]initWithText:@"My Message"] show]; 调用它。 show 函数会将视图添加到窗口中,因为我可以在屏幕中看到 myView 但其中没有显示 UiLabel。

标签: ios objective-c iphone uiview uilabel


【解决方案1】:

当您使用 self.frame 初始化您的 UILabel 时,您必须考虑其父视图中的值。

也许您的第二个参数:y = statusBarFrame.size.height 太高,这就是为什么您的标签不在您的视野范围内?

尝试使用 CGRectMake(0,0,self.frame.width, self.frame.height) 初始化您的标签

【讨论】:

  • 一开始我使用CGRectMake(0,0,self.frame.width, self.frame.height),但后来我认为超出了界限,所以我将其替换为[self frame];。当 [self frame] 被调用时,它返回自身的 X 和 Y 原点,所以我认为它不会超出界限。这两种方法都没有显示 UiLabel。
【解决方案2】:

检查您在标签中设置的文本是否为空白和

_messageLabel = [[UILabel alloc]initWithFrame:self.frame];

【讨论】:

  • 我也检查了运行时。标签未设置为空白。 message 是 NSString 并且它包含正确的字符串。我也试过 self.frame 但它不显示标签。
  • 尝试硬编码 _messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,20,20)];_messageLabel.text = @"asd";
  • 我试过了,但没有显示。我也尝试更改文本颜色,但它不起作用。
【解决方案3】:

我试过这段代码,它奏效了。确定您没有在其他地方为您的 UILabel 设置其他参数吗?这些是与您的代码的区别:

  • 我将您的“+”替换为“-”作为方法。
  • 我更改了用于初始化的 uilabel 框架。
  • 为标签添加了背景颜色(仅用于检查)。

    -(id)initWithText:(NSString *)text
    {
    CGRect 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]];
    
    NSString *message = [text copy];
    UILabel *_messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [_messageLabel setText:message];
    [_messageLabel setBackgroundColor:[UIColor lightTextColor]];
    [_messageLabel setAdjustsFontSizeToFitWidth:YES];
    [_messageLabel setTextAlignment:NSTextAlignmentJustified];
    [_messageLabel setTextColor:[ UIColor blackColor]];
    [self addSubview:_messageLabel];
    }
    return self;
    }
    

【讨论】:

    【解决方案4】:

    之前: 我曾经在我做后台操作的backgroundOP.m 文件中调用initWithText()show()。这些函数总是在不同的线程中执行。

    现在:我将调用移至委托方法内的 initWithText()show() 方法,并将委托添加到呈现 UIViewController 即我想要查看视图的 viewController.m 文件中出现。现在我从另一个文件(即来自backgroundOP.m)调用这个委托,问题就解决了。现在UIViewUILabel 都在屏幕上可见了。

    但我不明白为什么从后台线程函数添加 UIView 只显示 UIView 而不是它的内容/子视图。

    在我的 show() 函数中,我使用这个调度队列将 myView 添加到显示键窗口。就像下面的代码

    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    dispatch_async(dispatch_get_main_queue(), ^{
    [window addSubview:self]; // self represents myView object
     });
    

    如果有人知道这种行为的原因,请赐教。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多