更改 iOS App 中 UIRefreshControl 指示器的初始位置

sup*_*key 6 uitableview ios uirefreshcontrol

我正在使用 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];
Run Code Online (Sandbox Code Playgroud)

Tim*_*ich 7

一种解决方案是子类化UIRefreshControl并覆盖框架属性。以下代码适用于我测试的所有情况。您还可以通过应用变换来实现它而无需帧覆盖。从 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
    }
  }

}
Run Code Online (Sandbox Code Playgroud)


Dem*_*ese 6

直接设置框架不起作用,但您仍然可以使用 AutoLayout 来随时放置您的刷新控件。只是不要忘记设置translatesAutoresizingMaskIntoConstraintsfalse

例如,此代码将刷新控件设置在屏幕中间。

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
Run Code Online (Sandbox Code Playgroud)


UnR*_*ewa 1

很简单(斯威夫特)

   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
    }
Run Code Online (Sandbox Code Playgroud)