UILabel的文本没有显示在视图中

时间:2014-03-04 22:50:58

标签: 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];

由于

1 个答案:

答案 0 :(得分:1)

我复制了你的代码并试了一下。我可以看到标签“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;
}