ios - UILabel 的文本未显示在 View 中

标签 ios iphone uilabel

我正在创建一些标签,但它们没有出现。我的代码是这样的。有人可以帮帮我吗?

_scrollView.pagingEnabled = YES;
NSInteger numberOfViews = 3;

for (int i = 0; i < numberOfViews; i++)
{

    CGFloat xOrigin;
    if (i == 0)
    {
        xOrigin = i * self.view.frame.size.width;
    }
    else
    {
        xOrigin = i * (self.view.frame.origin.y+self.view.frame.size.width);
    }

   UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
   awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];

   UILabel *lbl1 = [[UILabel alloc] init];
   [lbl1 setFrame:CGRectMake(30, 30, 200, 100)];
   lbl1.backgroundColor=[UIColor clearColor];
   lbl1.textColor=[UIColor whiteColor];
   lbl1.userInteractionEnabled=NO;
   lbl1.text=[NSString stringWithFormat:@"Test Plan %d",i] ;
   [awesomeView addSubview:lbl1];


   [_scrollView addSubview:awesomeView];

谢谢

最佳答案

我复制了你的代码并试了一下。我可以看到标签“Test Plan 0”,但无法使用分页,因为您没有在 UIScrollView 上设置 contentSize 属性。

如果您设置 scrollView.contentSize = CGSizeMake(CGRectGetWidth(scrollView.frame) * (i + 1), CGRectGetHeight(scrollView.frame)); 您应该能够分页的结果你的看法。

工作示例:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.window.bounds];
    scrollView.pagingEnabled = YES;
    NSInteger numberOfViews = 3;

    for (int idx = 0; idx < numberOfViews; idx++)
    {
        CGRect awesomeViewFrame = self.window.frame;
        awesomeViewFrame.origin.x = idx * (CGRectGetMinX(self.window.frame) + CGRectGetWidth(self.window.frame));

        UIView *awesomeView = [[UIView alloc] initWithFrame:awesomeViewFrame];
        awesomeView.backgroundColor = [UIColor colorWithRed:0.5/idx green:0.5 blue:0.5 alpha:1];

        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 200, 100)];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor whiteColor];
        label.userInteractionEnabled = NO;
        label.text = [NSString stringWithFormat:@"Test Plan %d", idx] ;
        [awesomeView addSubview:label];

        [scrollView addSubview:awesomeView];

        scrollView.contentSize = CGSizeMake(CGRectGetWidth(scrollView.frame) * (idx + 1), CGRectGetHeight(scrollView.frame));
    }


    [self.window addSubview:scrollView];

    return YES;
}

关于ios - UILabel 的文本未显示在 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22184865/

相关文章:

javascript - 检测 iPad 或 iPhone(iOS6 和 iOS7)上是否安装了应用程序

ios - 如何在 iOS 编程中保持方法执行的顺序?

ios - 如何为多标签 View 设置标题栏?

ios - react 性 cocoa : How to prevent RACSignal from being executed twice in parallel?

iphone - RestKit:使用命名路由的 POST 请求

ios - UILabel 的 NSTextAlignmentCenter 不起作用

swift - 如何在 Xcode 6 中对齐标签下的按钮?

ios - UILabel 动画淡入淡出不起作用

php - 将图像从iOS上传到PHP脚本

iphone - 从带有自定义单元格的 UITableView 内的 UITextView 打开键盘时避免查看