Change the starting position of the UIRefreshControl indicator in your iOS app

I am developing an iOS app with UITableView and UIRefreshControl.

I would like to change the starting position of the UIRefreshControl indicator 50 pixels down from the default. I am writing the following code.

However, the original position does not change. it remains in its original position.

Could you please 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];

      

+3


source to share


3 answers


Simple (Swift)



   override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        refreshController.frame = CGRectMake(refreshController.bounds.origin.x,
                                              50.0,
                                              refreshController.bounds.size.width,
                                              refreshController.bounds.size.height);
        refreshController.superview?.sendSubviewToBack(refreshController) // for fix overlap tableview
    }

      

0


source


My solution is to use a custom class UIRefreshControl

with an overridden frame property. The following code works in all my cases. You can try to implement this without transform by applying transform. Setting frame to viewDidLayoutSubviews

does not work with 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
    }
  }

}

      

0


source


Setting the frame directly doesn't work, but you can still use AutoLayout to set the refreshControl at any time. Just remember to install translatesAutoresizingMaskIntoConstraints

in false

.

For example, this code sets refreshControl in the middle of the screen.

let refreshControl = UIRefreshControl()
collectionView.refreshControl = refreshControl
refreshControl.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint(item: refreshControl, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: refreshControl, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1.0, constant: 0).isActive = true

      

0


source







All Articles