2009-07-09 124 views
31

什麼是在股票和Spotlight中看到的整個UITableView的圓角的最佳方式?分組樣式不能解決問題,因爲圓角與單元格一起滾動。我試圖剪輯視圖,因此無論滾動位置如何,角落都是圓形的。UITableView的圓角

我看到了另一個關於如何對UIImage進行的討論,該UIImage建議使用另一個圖像來掩蓋它。我不確定這是否會起作用,因爲我需要點擊才能通過表格。這對我來說並不理想,因爲我希望背景圖案能夠透過角落展現出來。

+0

我不能相信這個七歲的問題仍然受到關注。我很久以前就放棄了這個功能,對不起,我自己無法測試和接受任何答案。我看到很多複選標記,謝謝大家!我很高興看到這有幫助。 – Scrollwheelie 2016-08-24 18:24:17

回答

2

您是否嘗試過「分組」表格視圖樣式?

self.tableView.style = UITableViewStyleGrouped; 

如需進一步參考,請參閱Table View Programming Guide。 「關於表視圖」一章有一些很好的截圖,描述了不同的風格。

+1

當你向上滾動表格時,它會用圓角掩蓋它,我想這就是他所指的。雖然這將是一個很好的開始,我猜,並使背景透明。 – Garrett 2009-07-09 23:11:31

+0

是的,這聽起來像你在這個正確的軌道上。 – 2009-07-09 23:12:19

+0

@Garrett:哦..剛開始我的股票應用程序,看看你的意思。直到現在真的沒有注意到這種效果..;-)對不起,我認爲他指的是簡單的分組表格視圖。 – 2009-07-09 23:14:43

64

這是一個古老的問題,但也許你仍然想知道如何做到這一點。

我轉載了一個像股票/聚光燈一樣的tableView。訣竅是

view.layer.cornerRadius = 10; 

對於這個工作,你需要將QuartzCore包括到您調用屬性的類:

#import <QuartzCore/QuartzCore.h> 

聽說因爲OS 3.0這僅適用。但是由於我的應用程序正在使用核心數據,所以它不是問題,因爲它已經用於OS 3.0和Hight。

我創建了一個自定義的UIView有一個子視圖與cornerRadius 10與

view.backgroundColor = [UIColor clearColor]; 

然後你必須把一個分組的UITableView風格在子視圖。您需要將backgroundColor設置爲clearColor,將separatorColor設置爲clearColor。然後,您必須將桌面視圖放置在圓角視圖內,這是通過設置框架大小和原點來完成的。我的loadView類我自定義的UIView是這樣的:

self.view = [[UIView alloc] init]; 
self.view.backgroundColor = [UIColor clearColor]; 

CustomUIViewClass *scherm = [[CustomUIViewClass alloc] init]; 

CGRect frame; 
frame.origin.x = 10; 
frame.origin.y = 50; 
frame.size.width = 300; 
frame.size.height = 380; 

scherm.frame = frame; 
scherm.clipsToBounds = YES; 
scherm.layer.cornerRadius = 10; 

[self.view addSubview:scherm]; 

CustomUITableViewClass *table = [[CustomUITableViewClass alloc] initWithStyle:UITableViewStyleGrouped]; 

frame.origin.y = -10; 
frame.origin.x = -10; 
frame.size.width = 320; 
frame.size.height = 400; 

table.tableView.frame = frame; 
[scherm addSubview:table.tableView]; 

我希望你明白我的英語,也許我會寫一個簡短的博客文章關於這項技術的一個樣本項目,將在這裏發佈的鏈接,當我準備。

+0

這是我在2010年9月通過測試的各種解決方案中唯一的解決方案,它在iOS 4和iOS 3.2上非常出色,並且性能卓越。將它集成到我的應用程序中。謝謝,漢斯! – DenTheMan 2010-09-01 21:18:05

2

我最近遇到了這個問題,並以不同的方式解決了它。以爲我會與大家分享結果。

我創建了一個帶有清晰圓角內部的矩形UIView,然後將它放在UITableView的頂部。你可以在my programming blog找到完整的描述。

它的工作原理與我想要的一樣。希望有所幫助。

55

更簡單的方法做,這是簡單地導入QuartzCore框架到您的項目。 #import <QuartzCore/QuartzCore.h>您tableViewController和剛剛成立

myTableView.layer.cornerRadius=5; 

這會給你圓潤的邊角,而無需你的tableview中添加到上海華盈或裁剪它。

2

那麼,有很多方法來解決這個問題。

但是,在我的情況下,所有都不能正常工作。我的桌子有時比桌子的尺寸小。

我會分享我的做法。我相信比以上的一些選擇更快捷。

使第一個和最後一個項目四捨五入。

  • 創建頂部(左|右)和底部(左|右)的CAShapeLayer

    shapeTop = [CAShapeLayer layer]; 
    shapeTop.path = [UIBezierPath 
    bezierPathWithRoundedRect:CGRectMake(0.0f, 0.0f, 306.0f, 58.0f) 
    byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight 
    cornerRadii:CGSizeMake(6.0f, 6.0f)].CGPath; 
    
    shapeBottom = [CAShapeLayer layer]; 
    shapeBottom.path = [UIBezierPath 
    bezierPathWithRoundedRect:CGRectMake(0.0f, 0.0f, 306.0f, 58.0f) 
    byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight 
    cornerRadii:CGSizeMake(6.0f, 6.0f)].CGPath; 
    
  • 該表需要背景色clearColor;

  • 細胞必須是彩色背景;
  • 設置它的layer.mask

    UIView* backgroundView = [[UIView alloc] initWithFrame:CGRectZero]; 
    backgroundView.backgroundColor = [UIColor whiteColor]; 
    cell.backgroundView = backgroundView; 
    
  • 不要忘記#import <QuartzCore/QuartzCore.h>

13

而是通過代碼黑客的,這裏是一個簡單的模仿分組風格。如果你想要的只是一個部分,這是有效的。

在界面生成器:

  • 集的UITableView風格爲純並與在左側和右側,也許具有x = 10,寬度= 300

然後設置一些填充框架拐角半徑和顏色自己:

#import <QuartzCore/QuartzCore.h> 

self.tableView.layer.borderColor = [UIColor colorWithWhite:0.6 alpha:1].CGColor; 
self.tableView.layer.borderWidth = 1; 
self.tableView.layer.cornerRadius = 4; 
0

下面給斯威夫特版本代碼:

let redColor = UIColor.redColor() 
self.tableView.layer.borderColor = redColor.colorWithAlphaComponent(0.9).CGColor 
self.tableView.layer.borderWidth = 1; 
self.tableView.layer.cornerRadius = 4; 

確保您在導入部分有import QuartzCore