16

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

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

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

这是一个示例屏幕截图,显示了在拉动刷新控件时显示的丑陋白色的表格:
在此处输入图像描述

4

3 回答 3

27

您必须使用 bgColor 创建一个视图,并在 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;
}
于 2012-11-15T09:51:32.743 回答
7

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
}

斯威夫特 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
}
于 2016-05-26T22:12:24.263 回答
5

现在可以通过简单地设置 UIRefreshControl 背景颜色属性来实现(Swift 5、iOS 13):

let ctrl = UIRefreshControl(frame: .zero)    
ctrl.backgroundColor = UIColor.gray'
tableView.refreshControl = ctrl

在此处输入图像描述

于 2020-03-13T14:00:01.723 回答