iosuitableviewuirefreshcontrol

Change the initial position of UIRefreshControl indicator in iOS App


I am developing iOS App with UITableView and UIRefreshControl.

I would like to change the initial position of UIRefreshControl indicator by 50px down from default. I write down the following code.

However, the initial position is not changed. it remains in its original position.

Could you tell me how to solve this problem?

CGFloat customRefreshControlHeight = 50.0f;
CGFloat customRefreshControlWidth = 320.0f;
CGRect customRefreshControlFrame = CGRectMake(0.0f,
                                              customRefreshControlHeight,
                                              customRefreshControlWidth,
                                              customRefreshControlHeight);
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] initWithFrame:customRefreshControlFrame];
refreshControl.tintColor = [UIColor blackColor];
[refreshControl addTarget:self action:@selector(onRefresh:) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:refreshControl];

Solution

  • One solution is to subclass UIRefreshControl and override frame property. The following code works in all the cases I tested. You can also implement it without frame override by applying transform. Setting frame in viewDidLayoutSubviews doesn't work since iOS 11.

    class OffsetableRefreshControl: UIRefreshControl {
    
      var offset: CGFloat = 64
      override var frame: CGRect {
        set {
          var rect = newValue
          rect.origin.y += offset
          super.frame = rect
        }
        get {
          return super.frame
        }
      }
    
    }