UIPickerView 框架大小 - 边距问题

发布于 2024-10-09 22:28:26 字数 2518 浏览 0 评论 0原文

我似乎无法在任何地方找到这个问题的答案 - 希望有人可以提供帮助!有谁知道如何确定 iOS 在选择器周围放置的边距?

我编写了一个通用视图,它在适当的位置显示带有多个数字和标签的 UIPickerView。 UIPickerView 是在界面生成器中创建的并填充整个屏幕。我目前正在使用 UIPickerView 的“frame”属性来确定选择器的宽度并计算各个组件的宽度和标签位置。

已更新 根据要求 - 我尝试包含代码。我正在构建的是一个通用的 UIViewController,它在关联的 .xib 文件中有一个选择器 - 每当我需要输入任何数值时,我都会将其加载为模式视图,只需为其提供必要的参数即可。选择器在 Interface Builder 中设置为“居中”模式 - 我尝试了其他一些,但似乎没有任何东西对布局产生任何影响。

该类接受位数和小数位数以及单位描述。然后,组件的数量由这些值确定,并将两个标签适当地放置在拾取器组件上以显示小数点和单位标签。 viewDidLoad方法如下所示。它生成在 UIPickerView 委托方法中使用的标准组件宽度。数学看起来一切都很好,但定位出了问题,我认为这是由于选择器边缘的边距造成的。

unitLabel.text = [NSString stringWithFormat:@"%@", unitDescription];
CGSize labelSize = [[unitLabel text] sizeWithFont:[unitLabel font]];
labelWidth = labelSize.width;

decimalPoint.text = [NSString stringWithFormat:@"."];
CGSize pointSize = [[decimalPoint text] sizeWithFont:[decimalPoint font]];
dpWidth = pointSize.width;
if (decimalPlaces == 0) {
    // Hide the decimal point
    decimalPoint.hidden = YES;
    dpWidth = 0;
}
else {
    decimalPoint.hidden = NO;
}

CGFloat pickerWidth = picker.frame.size.width;
//Now determine the standard component width
componentWidth = (pickerWidth - labelWidth - dpWidth) / (digits + decimalPlaces);   

//Now position the decimal point and label
//The components will be centred on the screen, so the location for the labels will be:
//Decimal point:
//Left position of picker + (componentWidth * digits)
//Unit:
//Left position of picker + (componentWidth * digits) + dpWidth + (componentWidth * decimalPlaces)
//Where:
//Left position of picker = screen width  -  (componentWidth * digits) + dpWidth + (componentWidth * decimalPlaces) + labelWidth
//                          ------------     -----------------------------------------------------------------------------------
//                                2                                 2
//
CGFloat leftPos = (picker.frame.size.width / 2) - (((componentWidth * digits) + dpWidth + (componentWidth * decimalPlaces) + labelWidth) / 2);
CGFloat dpPos = leftPos + (componentWidth * digits);
CGFloat labelPos = leftPos + (componentWidth * digits) + dpWidth + (componentWidth * decimalPlaces);

CGRect dpFrame = decimalPoint.frame;
dpFrame.origin.x = dpPos;
dpFrame.origin.y = picker.center.y - (5 * (pointSize.height / 12));
decimalPoint.frame = dpFrame;

CGRect labelFrame = unitLabel.frame;
labelFrame.origin.x = labelPos;
labelFrame.origin.y = picker.center.y - (5 * (labelSize.height / 12));
unitLabel.frame = labelFrame;

I can't seem to find an answer to this one anywhere - hoping someone can help! Does anyone know how to determine the margins that iOS puts around a picker?

I've written a generic view that displays a UIPickerView with a number of digits and labels in appropriate positions. The UIPickerView was created in interface builder and fills the whole screen. I'm currently using the "frame" property of the UIPickerView to determine the width of the picker and calculate the width of individual components and the label positions.

updated
As requested - I've tried to include the code. What I'm building is a generic UIViewController that has a single picker in the associated .xib file - I load it as a modal view whenever I need to input any numeric value, just by giving it the necessary parameters. The picker is set in Interface Builder to mode "Centered" - I've tried a few others but nothing really seems to make any difference to the layout.

The class takes in the number of digits and the number of decimal places as well as a unit description. The number of components is then determined by these values and two labels are placed appropriately over the picker components to show the decimal point and the unit label. The viewDidLoad method is shown below. It generates a standard component width that's used in the UIPickerView delegate methods. The maths all seems fine, but the positioning is going wrong and I think it's due to the margin round the edge of the picker.

unitLabel.text = [NSString stringWithFormat:@"%@", unitDescription];
CGSize labelSize = [[unitLabel text] sizeWithFont:[unitLabel font]];
labelWidth = labelSize.width;

decimalPoint.text = [NSString stringWithFormat:@"."];
CGSize pointSize = [[decimalPoint text] sizeWithFont:[decimalPoint font]];
dpWidth = pointSize.width;
if (decimalPlaces == 0) {
    // Hide the decimal point
    decimalPoint.hidden = YES;
    dpWidth = 0;
}
else {
    decimalPoint.hidden = NO;
}

CGFloat pickerWidth = picker.frame.size.width;
//Now determine the standard component width
componentWidth = (pickerWidth - labelWidth - dpWidth) / (digits + decimalPlaces);   

//Now position the decimal point and label
//The components will be centred on the screen, so the location for the labels will be:
//Decimal point:
//Left position of picker + (componentWidth * digits)
//Unit:
//Left position of picker + (componentWidth * digits) + dpWidth + (componentWidth * decimalPlaces)
//Where:
//Left position of picker = screen width  -  (componentWidth * digits) + dpWidth + (componentWidth * decimalPlaces) + labelWidth
//                          ------------     -----------------------------------------------------------------------------------
//                                2                                 2
//
CGFloat leftPos = (picker.frame.size.width / 2) - (((componentWidth * digits) + dpWidth + (componentWidth * decimalPlaces) + labelWidth) / 2);
CGFloat dpPos = leftPos + (componentWidth * digits);
CGFloat labelPos = leftPos + (componentWidth * digits) + dpWidth + (componentWidth * decimalPlaces);

CGRect dpFrame = decimalPoint.frame;
dpFrame.origin.x = dpPos;
dpFrame.origin.y = picker.center.y - (5 * (pointSize.height / 12));
decimalPoint.frame = dpFrame;

CGRect labelFrame = unitLabel.frame;
labelFrame.origin.x = labelPos;
labelFrame.origin.y = picker.center.y - (5 * (labelSize.height / 12));
unitLabel.frame = labelFrame;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文