簡體   English   中英

UILabel的文本未顯示在視圖中

[英]Text of UILabel not showing up in view

我正在創建一些標簽,但它們沒有出現。 我的代碼是這個。 有人可以幫我嗎?

_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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM