Question

I'm creating a UISwitch in TableViewCell.

On retina display it looks fine.

But when I'm building a project on 3GS my UISwitch looks like a badly painted picture.

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

My code

{
    ...
    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;
}
Was it helpful?

Solution

First off, it would be easier in this case to just set the switch as the accessoryView of the cell and let it worry about the positioning.

The reason you're seeing a blurry image is that your switch's frame has its origin on a half-pixel. You're setting the center of the switch, so where its origin is depends on the size of the switch (which is fixed, because UISwitch always uses a system-defined size). So say the switch has a size of 79 x 27 (the standard size), setting the center's y-coordinate to 20 would cause the frame origin's y-coordinate to be at 6.5 (20.0 - 27.0 * 0.5).

OTHER TIPS

Not sure what you mean by a "badly painted picture". Does it look blurry? It might be because its frame is not set to a full point.

What is the height of your cell? The following line may result in the UISwitch being set at 0.5 of a point:

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

That's fine on Retina because that's still a full pixel, but on non-Retina it's at 0.5 a pixel (since point = pixel on non-Retina and point = 2 pixels on Retina).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top