【问题标题】:why do my UILabels not appear in the right spaces为什么我的 UILabel 没有出现在正确的空间中
【发布时间】:2014-06-22 15:12:03
【问题描述】:

我有一个游戏。我正在加载 3 个带有标签对象的图像视图。然后我加载 5,然后加载 7。就在昨天,我遇到了同样的问题,我找到了将标签上的自动调整大小掩码设置为无的答案。好吧,我做到了,它修复了它,但现在问题又回到了今天。这很令人困惑,我不知道如何解决它。

我的问题是,如果我已经这样做了,为什么还会发生这种情况?我该如何解决?

这是我的代码:

  // random word from array of object
    NSString *randomWord = [self.objects objectAtIndex:randomObject];

    [self.imageViews[i] addSubview:self.labels[i]];
    [self.labels[i] setCenter:[self.imageViews[i] center]];

    // all labels setting text and alignment
    [self.labels[i] setText:randomWord];
    [self.labels[i] setTextAlignment:NSTextAlignmentCenter];

    // adding words to alphabetical array
    [self.alphabeticalWords addObject:randomWord];
    NSLog(@"%@", randomWord);
    //NSLog(@"%@", self.alphabeticalWords);

    [self.labels[i] sizeToFit];
    [self.labels[i] setAutoresizingMask:UIViewAutoresizingNone];
    [self.imageViews[i] setUserInteractionEnabled:YES];
    [self.imageViews[i] addGestureRecognizer:panGesture];

问题来了

非常感谢任何帮助。

【问题讨论】:

    标签: ios objective-c uiimageview uilabel


    【解决方案1】:

    如果视图层次结构和center,您使用的一个问题是。您将标签作为子视图添加到图像视图,然后使用center 尝试覆盖它们。中心的定义是:

    The center is specified within the coordinate system of its superview and is measured in points. Setting this property changes the values of the frame properties accordingly.

    将子视图的中心设置为其父视图的中心不一定会按照您想要的方式运行,因为它们不在同一个坐标空间中。

    您应该使用图像视图的bounds,它表示其内部坐标空间。您可能想要做的是这样的事情:

    [self.labels[i] setCenter:CGPointMake((self.imageViews[i].bounds.size.width / 2), (self.imageViews[i].bounds.size.height / 2)];
    

    【讨论】:

    • 我输入了那个,但它仍然表现得很奇怪。还有什么问题吗?
    • 它正在做与我的问题图片中完全相同的事情。由于某种原因,它并没有改变发生的事情。在标签上调用 sizeToFit 会导致它以这种方式行事吗?
    • 首先你是如何定位所有这些东西的?你能提供那个代码吗?
    • 我在我的故事板中设置了我想要的标签。然后我检查我的变量 self.carsLevel,它控制屏幕上的信息是 3、5 还是 7。如果是 3,我将标签/图像视图 4-7 隐藏。如果 self.carsLevel 为 5,我将标签/图像视图 6-7 隐藏。如果 self.carsLevel 为 7,我不会隐藏它们。我应该只在代码中设置位置吗?
    • 您是否在情节提要中关闭了自动布局?如果启用了自动布局,则使用自动调整蒙版并直接修改位置(通过框架或中心)将不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    相关资源
    最近更新 更多