UIRefreshControl Background Color

前端 未结 3 964
太阳男子
太阳男子 2021-02-04 06:02

Is it possible to make the background of a UIRefreshControl grow as the control grows?

I would like to have a colored background for the refresh control to match the top

3条回答
  •  礼貌的吻别
    2021-02-04 06:34

    Swift version for easier copy-pasting :)

    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
    }
    

提交回复
热议问题