LoginSignup
5
4

More than 5 years have passed since last update.

UIRefreshControlの位置をずらす

Posted at

もうすっかりお馴染みになった、ひっぱって更新を実現するUIRefreshControl。iOS6からUIKitに組み込まれて、実装もとても簡単になりました。
今回は、WebViewに組み込んだ際にインジケーターが表示される位置を下にずらしたい場合のTipsをメモとして残します。

環境

  • Xcode 7.3.1
  • Swift2.2

やり方

やり方としては、UIRefreshControlのサブクラスを作成して、layoutSubviewsをオーバーライドして実現します。

layoutSubviews() - Implement this method if you need more precise control over the layout of your subviews than either the constraint or autoresizing behaviors provide.

Appleのドキュメントには、制約またはオートサイズなど、サブビューのレイアウトをより正確に制御する必要がある場合に、このメソッドを実装します、とあります。

実装

実装はUIRefreshControlを継承したクラスを作成し、継承したクラスを実際には使います。下記は、Y軸方向にずらす位置があらかじめ決まったいる場合の簡単なサンプルとなります。

CustomRefreshController.swift
import UIKit

class CustomRefreshControl: UIRefreshControl {

     override func layoutSubviews() {
        var frame: CGRect = self.frame
        let moveY: CGFloat = 55.0
        frame.origin.y = frame.origin.y + moveY
        self.frame = frame
    }
}

簡単ですね。

謝辞

5
4
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
5
4