【问题标题】:Change the initial position of UIRefreshControl indicator in iOS App更改 iOS App 中 UIRefreshControl 指示器的初始位置
【发布时间】:2014-09-04 19:15:20
【问题描述】:

我正在使用 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];

【问题讨论】:

标签: ios uitableview uirefreshcontrol


【解决方案1】:

直接设置框架是行不通的,但你仍然可以随时使用 AutoLayout 来放置你的 refreshControl。只是不要忘记将translatesAutoresizingMaskIntoConstraints 设置为false

例如,此代码将 refreshControl 设置在屏幕中间。

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

【讨论】:

    【解决方案2】:

    我的解决方案是使用自定义 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
        }
      }
    
    }
    

    【讨论】:

    • 这对于UICollectionView 中的刷新控制非常有用!谢谢
    【解决方案3】:

    简单(斯威夫特)

       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
        }
    

    【讨论】:

      猜你喜欢
      • 2017-01-22
      • 2013-05-25
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      • 2020-09-20
      相关资源
      最近更新 更多