【问题标题】:UISwitch on non-retina screen非视网膜屏幕上的 UISwitch
【发布时间】:2012-11-07 14:28:04
【问题描述】:

我正在TableViewCell 中创建一个UISwitch

在视网膜显示器上看起来不错。

但是当我在 3GS 上构建项目时,我的 UISwitch 看起来像一幅画得很糟糕的图片。

http://iwheelbuy.com/stackoverflow/asdf.png

我的代码

{
    ...
    cell.textLabel.text = NSLocalizedString(@"settings model glare", nil);
    UISwitch *cellSwitch = [self switchWithTitle:@"glare"];
    [cellSwitch setCenter:CGPointMake(260.0f, cell.frame.size.height / 2)];
    [cell addSubview:cellSwitch];
    ...
}

- (UISwitch *) switchWithTitle:(NSString *)_title
{
    BOOL switchState;
    if ([[NSUserDefaults standardUserDefaults] boolForKey:_title] == NO)
    {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:_title];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    switchState = [[NSUserDefaults standardUserDefaults] boolForKey:_title];
    UISwitch *switchForCell = [[UISwitch alloc] initWithFrame:CGRectZero];
    if ([_title isEqualToString:@"glare"])
        [switchForCell addTarget:self action:@selector(changedValueForGlare) forControlEvents:UIControlEventValueChanged];
    else
        [switchForCell addTarget:self action:@selector(changedValueForShadow) forControlEvents:UIControlEventValueChanged];
    [switchForCell setOn:switchState];
    return switchForCell;
}

【问题讨论】:

  • 您可能需要上传问题的照片。很难说“画得不好是什么意思”。
  • 我上传了一张图片

标签: objective-c ios uiswitch iphone-3gs


【解决方案1】:

首先,在这种情况下,将开关设置为单元格的accessoryView 并让它担心定位会更容易。

您看到模糊图像的原因是您的开关框架的原点位于半像素上。您正在设置开关的center,因此其原点取决于开关的大小(这是固定的,因为UISwitch 始终使用系统定义的大小)。假设开关的尺寸为 79 x 27(标准尺寸),将中心的 y 坐标设置为 20 会导致框架原点的 y 坐标为 6.5 (20.0 - 27.0 * 0.5)。

【讨论】:

  • 非常感谢您指出附件视图问题!这是一个很好的解决方案
【解决方案2】:

不确定您所说的“画得不好”是什么意思。是不是看起来很模糊?可能是因为它的框架没有设置为一个完整的点。

你的单元格的高度是多少?以下行可能会导致 UISwitch 被设置为 0.5 个点:

[cellSwitch setCenter:CGPointMake(260.0f, cell.frame.size.height / 2)];

这在 Retina 上没问题,因为它仍然是一个完整的像素,但在非 Retina 上它是 0.5 个像素(因为点 = 非 Retina 上的像素和点 = 2 个 Retina 像素)。

【讨论】:

  • 这绝对与像素外和不正确的帧定位有关 ;)
  • 我用半像素测试过。看起来确实更好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多