UITableView的“反弹区域”中的浅灰色背景
苹果的iPhone应用程序,如音乐和Contants在UITableView中使用search栏。 当您向下滚动以便search栏向下移动时,滚动视图内容上方的空白区域将呈浅灰色背景色(请参阅截图)。

(请注意,search栏顶部有一个稍微更暗的边缘线,这不是默认的UISearchBar,但是子类化应该考虑到这一点)。
我尝试设置UITableView的背景颜色,但也会影响行。 有谁知道如何达到这个效果? 我将不得不重写实现drawRect:还是有内置的方式?
从iOS 7开始,您可以通过更改tableview背景视图来修补此问题。
[self.tableView setBackgroundView:view]; 使视图的背景颜色与您的父视图颜色相同。
设置透明胶片对性能不利。 你想要的是search栏上方的灰色区域,但它应该仍然是白色的,超出列表的末尾。
 你可以添加一个子视图到你的UITableView ,而不是内容。 
 CGRect frame = self.list.bounds; frame.origin.y = -frame.size.height; UIView* grayView = [[UIView alloc] initWithFrame:frame]; grayView.backgroundColor = [UIColor grayColor]; [self.listView addSubview:grayView]; [grayView release]; 
 如果你喜欢的话,你可以添加更多的花哨的东西,也许是淡入淡出,或者没有UISearchBar子类的分隔线。 
这是我非常喜欢的技巧之一。
 UIView *topview = [[[UIView alloc] initWithFrame:CGRectMake(0,-480,320,480)] autorelease]; topview.backgroundColor = [UIColor colorWithRed:226.0/255.0 green:231.0/255.0 blue:238.0/255.0 alpha:1]; [self.tableView addSubview:topview]; 
基本上你是创build一个大屏幕的大小,并把它放在内容区“上面”。 你将永远无法滚动过去。
不要担心320×480像素的UIView对内存的影响,因为CALayer没有任何有意义的内容,所以不会消耗任何显着的内存。
延续侯赛因的build议 :
Swift 3
 let bgView = UIView() bgView.backgroundColor = UIColor.white self.tableView.backgroundView = bgView 
目标C
 UIView *bgView = [UIView new]; bgView.backgroundColor = [UIColor whiteColor]; [self.tableView setBackgroundView:bgView]; 
在Swift中 (在iOS9上testing)
  let backView = UIView(frame: self.tableView.bounds) backView.backgroundColor = UIColor.clearColor() // or whatever color self.tableView.backgroundView = backView 
 此代码在Swift fot UITableView : 
 var frame = self.tableView.bounds frame.origin.y = -frame.size.height frame.size.height = frame.size.height frame.size.width = UIScreen.mainScreen().bounds.size.width let blueView = UIView(frame: frame) blueView.backgroundColor = UIColor.headerBlueColor() self.tableView.addSubview(blueView) 
我只find一种方法来做到这一点。 您必须将UITableView的backgroundColor设置为透明,将单元格的contentView的backgroundColor设置为您希望实际单元格的颜色,然后至关重要的是,您必须使浅灰色显示在UITableView的后面。 最后一步你可以通过设置UIWindow的backgroundColour,或者包含你的tableViewController。
所以,假设你有一个派生自UITableViewController的视图控制器,将这些行插入 – (void)viewDidLoad方法中: –
 // sets the background of the table to be transparent self.tableView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0]; // assuming we are inside a navigation or tab controller, set the background self.parentViewController.view.backgroundColor = [UIColor lightGrayColor]; 
然后在tableView:cellForRowAtIndexPath:创build新的单元格的部分内,添加: –
 // set an opaque background for the cells cell.contentView.backgroundColor = [UIColor whiteColor]; 
我刚刚遇到这个问题,自己find了解决办法。
原因
我使用Spark Inspector来检查表视图的布局 – 这真的有帮助。
事情是,在这种情况下,UITableView有3个子视图:
-  UITableViewWrapperView
-   UIView– 将backgroundColor设置为浅灰色
-  UISearchBar
 在向下滑动tableview内容时,第二个子视图高度将dynamic增加,以填充UITableViewWrapperView和UITableView框架原点之间的空间。 
解
 设置backgroundColor或backgroundView属性不会影响第二个子视图。 你需要做的是find第二个视图,并改变它的颜色,如下所示: 
 if (_tableView.subviews.count > 1) { _tableView.subviews[1].backgroundColor = THE_TARGET_COLOR; } 
 在我的情况下,我需要所有视图都是白色的,所以我使用了下面这个不太容易被苹果公司的UITableView视图层次结构的未来变化: 
 for (UIView *subview in _tableView.subviews) { subview.backgroundColor = [UIColor whiteColor]; } 
 我会给你最好的方式来做到这一点。 
 首先将表视图的背景颜色设置为您在界面构build器中所需的颜色。 
 然后响应这个UITableView委托tableView:willDisplayCell:ForIndexPath:方法 
 - (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCelll*)cell forIndexPath:(NSINdexPath*)indexPath { [cell setBackgroundColor:[UIColor whiteColor]]; } 
 另一种方法是: 
 在ViewDidLoad方法(或者你喜欢的任何地方)设置tableView背景颜色来清除颜色,如下所示: 
 self.tableView.backgroundColor = [UIColor clearColor]; 
然后将超级视图颜色设置为白色
 self.tableView.superview.backgroundColor = [UIColor whiteColor]; 
最简单的解决scheme
在表视图的弹跳区域的底部和顶部创build不同颜色的最简单方法是设置表视图的关键tableHeaderBackgroundColor 。 这样做你设置顶部的颜色。 我不确定,但也许还有另一个关键的脚注,看看。 如果你没有find任何东西,你只需要在底部显示你想要显示的颜色的表格背景。 上面你可以看到一个示例代码:
 self.table.setValue(UIColor.blue , forKey: "tableHeaderBackgroundColor") 
希望它能帮助你。 如果是的话,让其他人知道这个简单的方法给予回答:)
我不认为你想重写drawRect。 最有可能的是你看到的是另一个视图或窗口的背景颜色,它位于表格视图的“后面”(即是超视图)。 在苹果的UI小部件中通常有相当复杂的UIViews层。 探索GDB中的视图层次结构,查看[myView superview],然后[someSuperView子视图],然后尝试在debugging器中操作它们的BG颜色,看看是否可以find它是哪一个。 但是,如果您采用这种方式进行修复,请注意它可能不兼容。
您也可以尝试在Interface Builder(或窗口本身)中设置tableview背后的一个视图的BG颜色。
如果您使用的是tableviewcell,则可以将视图背景设置为不透明的白色。 然后使用
 self.tableView.backgroundColor = [UIColor grayColor]; 
在视图中加载方法。
我相信那是[UITableView backgroundColor]。 您已经影响了行,因为行具有backgroundColor ==清除(或半透明)。 所以,如果你将行非透明,所有将正常工作。 这将是解决scheme。
我遵循Peylow概述的技巧,通过简单地添加一个子视图来获得UITableView。 我从代码中唯一的变化就是在苹果应用程序中使用了一个更接近苹果的颜色,另外我通过将框架原点y坐标减less一个像素,使它与UISearchbar上方的线条更接近一点:
 frame.origin.y = -frame.size.height - 1 
对于任何人想知道如何在底部反弹区域做同样的事情:
首先添加一个你想要的背景色的子视图到你的表视图的背景视图中:
 self.bottomView = [[UIView alloc] initWithFrame:CGRectOffset(self.tableView.frame, 0, self.tableView.frame.size.height)]; self.bottomView.backgroundColor = whateverColorYouLike; [self.tableView.backgroundView addSubview:self.bottomView]; 
然后在你的表格视图的委托中:
 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGRect frame = self.bottomView.frame; frame.origin.y = self.tableView.contentSize.height - self.tableView.contentOffset.y; self.bottomView.frame = frame; }