2010-06-24 21 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) 來修復它。

相關問題