デフォルトでは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を使ったアプリを作っています。
機械学習関連の情報を発信しています。