LoginSignup
1
1

More than 3 years have passed since last update.

デフォルトではCollectionViewの上部に出てくるUIRefreshControl。
これの位置を変えたい。
ふつうにframeで設定できるんじゃないかと思ってやってみると

let refreshControl = UIRefreshControl()
collectionView.refreshControl = refreshControl
refreshControl.frame = CGRect(x: 0, y: 100, width: collectionView.bounds.width, height: 60)

位置変わらない。。。。。

どうすればいいかというと、

コンテナになるviewを作って位置設定して、refreshControlをaddSubViewすると機能する。

let refreshControl = UIRefreshControl()
collectionView.refreshControl = refreshControl
let containerView = UIView(frame: CGRect(x: 0, y: 100, width: collectionView.bounds.width, height: 60))
collectionView.addSubview(containerView) // 1.
containerView.addSubView(refreshControl) // 2.
// refreshControlをコンテナにaddSubViewする前にコンテナを
// collectionViewやtableViewにaddSubViewしないとrefreshControlが表示されない。
refreshControl.addTarget(self, action: #selector(refreshFunc), for: .valueChanged)

refreshControlをコンテナにaddSubViewする前にコンテナをcollectionViewやtableViewにaddSubViewしないとrefreshControlが表示されないので注意。

🐣


フリーランスエンジニアです。
お仕事のご相談こちらまで
rockyshikoku@gmail.com

Core MLを使ったアプリを作っています。
機械学習関連の情報を発信しています。

Twitter
Medium

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1