gpt4 book ai didi

ios - 为什么我的 UILabels 没有出现在正确的空间中

转载 作者:行者123 更新时间:2023-11-29 02:52:20 24 4
gpt4 key购买 nike

我有一个游戏。我正在加载 3 个带有标签对象的 ImageView 。然后我加载 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];

这就是问题

enter image description here

非常感谢任何帮助。

最佳答案

一个问题是您使用 View 层次结构和中心。您正在将标签作为 subview 添加到 ImageView ,然后使用 center 尝试覆盖它们。中心的定义是:

中心在其父 View 的坐标系内指定,并以点为单位进行测量。设置此属性会相应地更改框架属性的值。

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

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

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

关于ios - 为什么我的 UILabels 没有出现在正确的空间中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24352572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com