UIPickerView框架大小 - 保证金问题

时间:2010-12-30 17:46:28

标签: iphone border uipickerview margin frame

我似乎无法在任何地方找到答案 - 希望有人可以提供帮助!有谁知道如何确定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;

0 个答案:

没有答案