UIScrollViewをコードだけで作る – Swift

環境

  • swift
  • swift3
  • swift4
  • xcode9
  • UIScrollView
  • コード

実装

    var scrollview = UIScrollView()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let viewX = self.view.frame.width
        let viewY = self.view.frame.height
        scrollview.frame = CGRect(x:0, y:0, width:viewX , height:viewY/2)
        scrollview.backgroundColor = UIColor.red
        scrollview.contentSize = CGSize(width:viewX * 2 , height:viewY/2)
        self.view.addSubview(scrollview)
    }

実行

on.gif