UIRefreshControl背景颜色

是否有可能使UIRefreshControl的背景随着控件的增长而增长?

我想有一个彩色的背景刷新控制匹配顶部单元格的背景颜色。 改变tableview的背景颜色是不可接受的,因为在底部的空单元格也会有颜色,但我需要它们保持白色。

苹果的邮件应用程序显示这种行为。 刷新控件的背景匹配灰色的search栏,但表格视图底部的空单元格仍然是正常的白色。

下面是一个示例屏幕截图,其中显示了如何在刷新控件时显示丑陋的白色:
在这里输入图像说明

你必须用bgColor创build一个视图,然后在tableView中添加负y原点。

警告:

  • 你必须在tableView子视图的底部插入这个视图
  • 设置完refreshControll后,你必须插入这个视图:self.refreshControl = refreshControl;

如果你不这样插入这个视图,你将不会看到刷新控件:他将被隐藏在你的视图之下。

- (void)viewDidLoad { [super viewDidLoad]; // Background Color UIColor *bgRefreshColor = [UIColor grayColor]; // Creating refresh control refreshControl = [[UIRefreshControl alloc] init]; [refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged]; [refreshControl setBackgroundColor:bgRefreshColor]; self.refreshControl = refreshControl; // Creating view for extending background color CGRect frame = self.tableView.bounds; frame.origin.y = -frame.size.height; UIView* bgView = [[UIView alloc] initWithFrame:frame]; bgView.backgroundColor = bgRefreshColor; bgView.autoresizingMask = UIViewAutoresizingFlexibleWidth; // Adding the view below the refresh control [self.tableView insertSubview:bgView atIndex:0]; // This has to be after self.refreshControl = refreshControl; } 

Swift版本更容易复制粘贴:)

Swift 2

 func viewDidLoad() { super.viewDidLoad() // Background Color let backgroundColor = UIColor.grayColor() // Creating refresh control let refresh = UIRefreshControl() refresh!.backgroundColor = backgroundColor refresh!.addTarget(self, action: #selector(refresh), forControlEvents: UIControlEvents.ValueChanged) refreshControl = refresh // Creating view for extending background color var frame = tableView.bounds frame.origin.y = -frame.size.height let backgroundView = UIView(frame: frame) backgroundView.autoresizingMask = .FlexibleWidth backgroundView.backgroundColor = backgroundColor // Adding the view below the refresh control tableView.insertSubview(backgroundView, atIndex: 0) // This has to be after refreshControl = refresh } 

Swift 3

 func viewDidLoad() { super.viewDidLoad() // Background Color let backgroundColor = .gray // Creating refresh control let refresh = UIRefreshControl() refresh!.backgroundColor = backgroundColor refresh!.addTarget(self, action: #selector(refresh), forControlEvents: .valueChanged) refreshControl = refresh // Creating view for extending background color var frame = tableView.bounds frame.origin.y = -frame.size.height let backgroundView = UIView(frame: frame) backgroundView.autoresizingMask = .flexibleWidth backgroundView.backgroundColor = backgroundColor // Adding the view below the refresh control tableView.insertSubview(backgroundView, atIndex: 0) // This has to be after refreshControl = refresh }