【问题标题】:Why the UILabel is not displaying?为什么 UILabel 不显示?
【发布时间】:2012-06-29 03:21:51
【问题描述】:

在我的appDelegate.m 我有这个代码,但UILabel 没有显示,而只有UIImageView。

为什么?

notificationView = [[UIImageView alloc] initWithFrame: CGRectMake(154, 320, 140, 47)];
notificationView.image = [UIImage imageNamed:@"notification.png"];

NSString *message = [NSString stringWithString:@"New Videos"];

notificationLabel = [[UILabel alloc] initWithFrame: CGRectMake(165, 313, 140, 47)]; 
notificationLabel.font = [UIFont boldSystemFontOfSize:12];
notificationLabel.textAlignment = UITextAlignmentLeft;
notificationLabel.text = [NSString stringWithFormat:@"%d %@", numberOfVideos, message];
notificationLabel.textColor = [UIColor whiteColor];
notificationLabel.backgroundColor = [UIColor clearColor];

notificationView.alpha = 0.0;
notificationLabel.alpha = 0.0;

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
notificationView.alpha = 1.0;
notificationLabel.alpha = 1.0;
[UIView commitAnimations];

[self.window addSubview: notificationView];
[self.notificationView addSubview: notificationLabel];

[UIView commitAnimations];  

[notificationView release];
[notificationLabel release];

【问题讨论】:

  • 你的 notificationView(image) 是否正在显示
  • 检查 imageView 的宽度和标签的 x 轴

标签: iphone objective-c cocoa-touch uiimageview uilabel


【解决方案1】:

通知视图的宽度为140。您的标签被放置在通知视图中165 的 X 位置,该位置超出了通知视图的范围。所以它是不可见的。

【讨论】:

  • 我已经在 CGRectMake(154, 320, 165, 47) 处设置了 UIImageView 和 UILabel,但仍然没有显示 UILabel。
【解决方案2】:

notificationLabel 是 notificationView 的子视图。 它被放置在其超级视图的范围之外。

试试

notificationLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 140, 47)]; 

然后从那里出发。对于所有子视图,超级视图的左上角为 (0, 0)。

【讨论】:

  • 现在可以正常使用了。感谢您的 CGRectMake(0, 0, 140, 47) 提示!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-12
  • 2016-02-16
相关资源
最近更新 更多