2010-06-24 48 views
1

的UISwitch我的设备上:Switch Image with the bottom pixels cut off http://gorgando.com/uiswitch.jpg底部被切断的设备上,但显示正常的模拟器

的UISwitch在模拟器上:Good UISwitch http://gorgando.com/uiswitch-good.png

正如你所看到的,底部像素在设备上切断,但不在模拟器上。我尝试过所有我能想到的事情,但没有解决问题。

一些我试过的东西:

  • 更改UISwitch的框架的高度
  • 改变UICell的高度
  • 更改UICell的内容查看的高度
  • 添加UISwitch到UICell而不是UICell的内容查看

以下是相关代码:
这是的UITableViewController的viewDidLoad中:

UISwitch *sw = [[UISwitch alloc] init]; 
self.contactedSwitch = sw; 
[sw release]; 
self.contactedSwitch = [UISwitch switchWithLeftText:@"YES" andRight:@"NO"]; 
self.contactedSwitch.center = CGPointMake(230, 22); 
self.contactedSwitch.on = [self.contact.contacted boolValue]; 

这就是switchWithLeftText:andRight方法来源于:

#import "UISwitch-Extended.h" 

#define TAG_OFFSET 900 

@implementation UISwitch (tagged) 
- (void) spelunkAndTag: (UIView *) aView withCount:(int *) count 
{ 
    for (UIView *subview in [aView subviews]) 
    { 
     if ([subview isKindOfClass:[UILabel class]]) 
     { 
      *count += 1; 
      [subview setTag:(TAG_OFFSET + *count)]; 
     } 
     else 
      [self spelunkAndTag:subview withCount:count]; 
    } 
} 

- (UILabel *) label1 
{ 
    return (UILabel *) [self viewWithTag:TAG_OFFSET + 1]; 
} 

- (UILabel *) label2 
{ 
    return (UILabel *) [self viewWithTag:TAG_OFFSET + 2]; 
} 

+ (UISwitch *) switchWithLeftText: (NSString *) tag1 andRight: (NSString *) tag2 
{ 
    UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 94, 27)]; 

    int labelCount = 0; 
    [switchView spelunkAndTag:switchView withCount:&labelCount]; 

    if (labelCount == 2) 
    { 
     [switchView.label1 setText:tag1]; 
     [switchView.label2 setText:tag2]; 
    } 

    return [switchView autorelease]; 
} 

@end 

这是我的UISwitch添加到我的tableviewcell:

[[contactedCell contentView] addSubview:self.contactedSwitch]; 

非常感谢!

[更新]我认为tableviewcell的可能是问题,所以我将这些UISwitches添加到常规的UIView中,以查看它们的外观。我有完全相同的问题,他们在模拟器中看起来没问题,底部在设备中被切碎。太奇怪了!

+0

您不能更改UISwitch的默认大小,因为它只会忽略您的代码。您可以尝试增加单元格大小并将其从边缘移开。如果仔细观察,看起来顶部会被模拟器截断,底部会被夹在设备上。所以无论哪种方式它都会被裁剪掉。 – iwasrobbed 2010-06-24 19:36:00

+0

我只是将开关添加到常规的UIView并得到相同的结果。它们在模拟器中看起来不错,但底部仍然在设备上被切断。我猜这消除了UITableViewCell不够大,因为这些UISwitch在它们周围都有足够的空间。 – Brad 2010-06-24 20:58:46

回答

1

从来没有弄清楚究竟出了什么问题,但我最终在IB中创建了UISwitch,并使它在这种情况下工作得很漂亮。非常奇怪,它在IB中工作,而不是以编程方式,当我基本上做同样的事情。

3

编程创建UISwitch并更改其框架的原点时,我遇到了同样的问题 - 原来是半像素问题。

注意半像素错误的一个关键是:在模拟器vs设备上不需要的工件出现不同吗?

您将UISwitch与Interface Builder放置在一起的解决方案修复了这个问题 - 如果您正在设置UISwitch框架的原点x/y,您还可以确保floor()为新坐标。

UISwitch *mySwitch = [[UISwitch alloc] initWithFrame(CGRectZero)]; 
CGRect switchFrame = mySwitch.frame; 
// move frame up without pixel fractions 
switchFrame.origin.y = floor((cell.contentView.frame.size.height - switchFrame.size.height)/2); 
mySwitch.frame = switchFrame; 
[cell.contentView addSubview:mySwitch]; 

在原来的海报的情况下,uiswitch的高度为奇数(27个像素),因此设置的中心到22将高度为13.5。 UISwitch的原点y坐标变为22-13.5 = 8.5像素。请不要通过设置中心点或移动坐标来移动UISwitch,也不要在调用CGPointMake(230,22.5)时使用一小部分。

追踪此类错误的另一种方法是通过Interface Builder寻找“.5”坐标。我发现有时在Interface Builder中过度调整UI元素的位置会引入此错误。

2

这是造成UISwich顶部。 您可以使用UISwich.origin.y = ceil((height - UISwich.size.height)/2) 来修复它。