LoginSignup
0
0

More than 1 year has passed since last update.

横にページングするScrollViewを作成(UIScrollView)

Posted at

今回の内容

40DE6522-01CF-4CF2-9D2A-74BF6AD45650_1_201_a.jpeg 5859CC6F-616D-45CA-B4AE-C744B5A36F9F_1_201_a.jpeg AEA980AA-097C-447E-BF5E-44470B16650B_1_201_a.jpeg

コードと簡単解説

  • 横にだけスクロールさせたいので、.contentSizeCGSize(width: view.frame.width * 3, height: view.frame.height)を設定します。

  • ページングさせるので、.isPagingEnabled = trueで設定します。

import UIKit

class ViewController: UIViewController {

    let scrollView = UIScrollView()

    override func viewDidLoad() {
        super.viewDidLoad()

        scrollView.frame = CGRect(x: view.frame.minX, y: view.frame.minY, width: view.frame.width, height: view.frame.height)
        scrollView.contentSize = CGSize(width: view.frame.width * 3, height: view.frame.height)
        scrollView.isPagingEnabled = true
        scrollView.backgroundColor = .systemIndigo
        view.addSubview(scrollView)

        let leftView = UIView(frame: CGRect(x: view.frame.minX, y: view.frame.minY, width: view.frame.width, height: view.frame.height))
        leftView.backgroundColor = .systemGreen
        scrollView.addSubview(leftView)

        let centerView = UIView(frame: CGRect(x: view.frame.maxX, y: view.frame.minY, width: view.frame.width, height: view.frame.height))
        centerView.backgroundColor = .systemRed
        scrollView.addSubview(centerView)

        let rightView = UIView(frame: CGRect(x: view.frame.maxX * 2, y: view.frame.minY, width: view.frame.width, height: view.frame.height))
        rightView.backgroundColor = .systemTeal
        scrollView.addSubview(rightView)
    }

}

終わり

ご指摘、ご質問などありましたら、コメントまでお願い致します。

0
0
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
0
0