【问题标题】:UIRefreshControl Background ColorUIRefreshControl 背景颜色
【发布时间】:2012-11-02 02:26:19
【问题描述】:

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

我希望刷新控件的背景颜色与顶部单元格的背景颜色相匹配。更改 tableview 的背景颜色是不可接受的,因为底部的空单元格也会有颜色,但我需要它们保持白色。

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

这是一个示例屏幕截图,显示了在拉动刷新控件时显示的丑陋白色:

【问题讨论】:

    标签: iphone objective-c ios uirefreshcontrol


    【解决方案1】:

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

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

    【讨论】:

    • 视觉上看起来比选择的答案差(在拖动开始和返回动画时有一些伪影)
    【解决方案2】:

    您必须使用 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;
    }
    

    【讨论】:

    • 你能解释一下你的警告吗?我认为这可以改善你的答案,知道你为什么警告我们。
    • 完成!这是为了避免你的刷新控件被你添加的 backgroundView 隐藏
    • +1 感谢它只是增加了细节并有助于向正在学习的新人解释。
    • 谢谢亚历克斯!这正是我所需要的。
    • 如果您的视图支持旋转,那么您可能需要在bgView 上添加autoresizingMask 以确保它可以适当地拉伸。 bgView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    【解决方案3】:

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      • 2015-11-29
      相关资源
      最近更新 更多