ios8似乎忽略了UILabel的X和Y坐标。

时间:2022-06-04 21:20:01

I have a somewhat old Objective C function that adds a UILabel to my UIView. It keeps track of the last label's Y in the variable _nextFieldY so it can put the next label below it. When I'm adding the label, I init it with its Width, Height, X and Y, and add it to my view. In iOs 7, this works, and correctly puts everything in its place. However, with any devices running the same app in iOs 8+, it appears the labels are all put at (0,0); the labels are all on top of each other in the top left corner. Was there a change between these versions that would cause this? I usually avoid programmatically adding things to my XIBs, so I don't know if there's certain work I have to do to get them to behave.

我有一个有点老的Objective - C函数,它将UILabel添加到UIView中。它跟踪变量_nextFieldY中的最后一个标签的Y,以便将下一个标签放在它下面。当我添加标签时,我初始化它的宽度,高度,X和Y,并将它添加到我的视图中。在ios7中,这是可行的,并且正确地将所有东西放在它的位置上。然而,在ios8 +中运行同一应用程序的任何设备,其标签都显示为(0,0);标签都是在左上角互相重叠的。这些版本之间是否会发生变化?我通常避免以编程的方式向我的xib中添加东西,所以我不知道是否需要做某些工作才能使它们正常运行。

CGFloat labelWidth = 100;
CGFloat labelHeight = 40;
CGFloat labelY = _nextFieldY-REG_FORM_PADDING_Y;
UIFont *labelFont = [UIFont boldSystemFontOfSize:12.0f];

UILabel *currLabel = [[UILabel alloc] initWithFrame:CGRectMake(REG_FORM_OFFSET_X, labelY, labelWidth, labelHeight)];
currLabel.text = label;
[currLabel setTranslatesAutoresizingMaskIntoConstraints:NO];

[currLabel setFont:labelFont];

[currLabel setTextAlignment:NSTextAlignmentLeft];

[_formView addSubview:currLabel];

_nextFieldY += currLabel.frame.size.height + REG_FORM_PADDING_Y;

1 个解决方案

#1


1  

Since you're setting the frame directly to position your view, you should be leaving translatesAutoresizingMaskIntoConstraints set to YES. Setting it to NO tells the system that it can ignore the frame you set.

因为要直接设置框架来定位视图,所以应该将translatesautoresizingmaskintoconstraint设置为YES。设置它不告诉系统它可以忽略你设置的帧。

#1


1  

Since you're setting the frame directly to position your view, you should be leaving translatesAutoresizingMaskIntoConstraints set to YES. Setting it to NO tells the system that it can ignore the frame you set.

因为要直接设置框架来定位视图,所以应该将translatesautoresizingmaskintoconstraint设置为YES。设置它不告诉系统它可以忽略你设置的帧。