【问题标题】:Can't get UILabels to appear on UIViews created dynammically无法让 UILabels 出现在动态创建的 UIViews 上
【发布时间】:2014-08-29 16:58:49
【问题描述】:

尝试做一些相当简单的事情。我正在创建一行矩形,它们放置在水平滚动的UIScrollView 中。矩形布局完美。但是,每个矩形上都有一个UILabel。仅显示第一个标签。其他都不是。这里的红色矩形是基本的CGRects。黄色是让我调试标签是否绘制正确。如您所见,仅显示第一个标签:

self.dayButtonsArray = [[NSMutableArray alloc] initWithCapacity:21];

CGFloat buttonSpacing = 2.0f;
CGFloat buttonWidth = 50.0f;
CGFloat buttonHeight = self.contentView.frame.size.height;
CGFloat startX = self.contentView.frame.origin.x;
CGFloat thisX = startX;

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

    CGRect frame = CGRectMake(thisX, self.contentView.frame.origin.y, buttonWidth, buttonHeight);
    UIView *thisButton = [[UIView alloc] initWithFrame:frame];

    // set up a label for each view
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    [label setTextColor:[UIColor blackColor]];
    label.font = [UIFont fontWithName:DEFAULT_FONT size:14];
    label.textAlignment = NSTextAlignmentCenter;
    [label setText:[NSString stringWithFormat:@"DAY %d", i+1]];
    [labelsArray addObject:label];

    [thisButton setBackgroundColor:[UIColor redColor]];

    // now add these elements
    [thisButton addSubview:label];
    [thisButton bringSubviewToFront:label];
    [self.contentView addSubview:thisButton];
    [self.contentView bringSubviewToFront:thisButton];
    [self.dayButtonsArray addObject:thisButton];

    CGRect contentSize = self.contentView.frame;
    contentSize.size.width = ((i+1) * buttonWidth) + (i * buttonSpacing);
    self.contentView.frame = contentSize;

    thisX += buttonWidth + buttonSpacing;
}

【问题讨论】:

  • 我认为您可以在循环之后设置一次contentSize.size.width,而不是在每次迭代时设置它。
  • 同意,除了 int 的值是循环的本地值。无论哪种方式,简单的解决方案。

标签: ios objective-c uiview uilabel


【解决方案1】:

您对 UIView 和 UILabel 使用相同的 frame。

CGRect frame = CGRectMake(thisX, self.contentView.frame.origin.y, buttonWidth, buttonHeight);
UIView *thisButton = [[UIView alloc] initWithFrame:frame];

// set up a label for each view
UILabel *label = [[UILabel alloc] initWithFrame:frame]; <-- this frame should have an origin.x of 0.0

label 的 x 原点应参考 thisButton 进行调整,因为您将其添加为子视图。

【讨论】:

  • 好主意。我尝试了UILabel *label = [[UILabel alloc] initWithFrame:thisButton.frame];,但结果相同。如果我将标签设为 contentView 的子视图,那么它可以工作,但我还有一个点击手势识别器,恐怕它可能不起作用。很奇怪。
  • @usr55410 您仍在使用 UIView 的原点设置 UILabel 的原点。使用这个:UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, thisButton.frame.size.width, thisButton.frame.size.height)];
【解决方案2】:

UILabel 中的 x origin 不会在循环中移动。所以这意味着你正在创建所有的UILabel

试试这个:

float buttonWidth = 50.0f;
float buttonHeight = 50.0f;


for (int i = 0; i < 21; i++)
{
    //note that the width is multiplying the i
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(buttonWidth*i, 20, buttonWidth, buttonHeight)];
    [label setText:[NSString stringWithFormat:@"DAY %d", i+1]];
    [label setTextColor:[UIColor blackColor]];
    [self.view addSubview:label];
}

【讨论】:

  • x 原点的值随着每次通过时重新定义的框架而移动。并不是说我让它工作或任何事情......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-10
  • 1970-01-01
  • 2020-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多