更改iOS应用程序中UIRefreshControl指示灯的初始位置

更改iOS应用程序中UIRefreshControl指示灯的初始位置,ios,uitableview,uirefreshcontrol,Ios,Uitableview,Uirefreshcontrol,我正在开发带有UITableView和UIRefreshControl的iOS应用程序 我想将UIRefreshControl指示器的初始位置从默认值向下更改50px。我写下下面的代码 但是,初始位置不变。它保持原来的位置 你能告诉我怎么解决这个问题吗 CGFloat customRefreshControlHeight = 50.0f; CGFloat customRefreshControlWidth = 320.0f; CGRect customRefreshControlFrame =

我正在开发带有UITableView和UIRefreshControl的iOS应用程序

我想将UIRefreshControl指示器的初始位置从默认值向下更改50px。我写下下面的代码

但是,初始位置不变。它保持原来的位置

你能告诉我怎么解决这个问题吗

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];
简单(快速)


我的解决方案是使用带有重写框架属性的自定义
UIRefreshControl
类。以下代码适用于我的所有情况。您可以尝试通过应用transform来实现它,而无需进行转换。自iOS 11以来,在viewDidLayoutSubviews中设置帧不起作用

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
    }
  }

}

直接设置框架不起作用,但您仍然可以随时使用AutoLayout放置refreshControl。只是别忘了将
翻译自动调整大小GMaskintoConstraints
设置为
false

例如,这个代码将刷新控制设置在屏幕中间。

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

我想你可以在这里找到一个合适的答案:
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