facebook twitter hatena line email

Ios/swift/UIScrollView

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

UIScrollViewとは

横スクロールを作れるUI

サンプル

-ViewController.swift

       let myScrollView = UIScrollView()
       myScrollView.showsHorizontalScrollIndicator = false // スクロールバーを削除
       myScrollView.frame = CGRectMake(0, 50, self.view.frame.size.width, 100)
       
       let button1 = UIButton()
       button1.frame = CGRectMake(0, 0, self.view.frame.size.width / 3, 100)
       button1.setTitle("ほげほげ1", forState: .Normal)
       button1.setTitleColor(UIColor.redColor(), forState: .Normal)
       button1.addTarget(self, action: #selector(self.drawerOnClickButton(_:)), forControlEvents: .TouchUpInside)
       myScrollView.addSubview(button1)
       
       let button2 = UIButton()
       button2.frame = CGRectMake(self.view.frame.size.width / 3, 0, self.view.frame.size.width / 3, 100)
       button2.setTitle("ほげほげ2", forState: .Normal)
       button2.setTitleColor(UIColor.redColor(), forState: .Normal)
       button2.addTarget(self, action: #selector(self.drawerOnClickButton(_:)), forControlEvents: .TouchUpInside)
       myScrollView.addSubview(button2)
       
       let button3 = UIButton()
       button3.frame = CGRectMake(self.view.frame.size.width / 3 * 2, 0, self.view.frame.size.width / 3, 100)
       button3.setTitle("ほげほげ3", forState: .Normal)
       button3.setTitleColor(UIColor.redColor(), forState: .Normal)
       button3.addTarget(self, action: #selector(self.drawerOnClickButton(_:)), forControlEvents: .TouchUpInside)
       myScrollView.addSubview(button3)
       
       let button4 = UIButton()
       button4.frame = CGRectMake(self.view.frame.size.width / 3 * 3, 0, self.view.frame.size.width / 3, 100)
       button4.setTitle("ほげほげ4", forState: .Normal)
       button4.setTitleColor(UIColor.redColor(), forState: .Normal)
       button4.addTarget(self, action: #selector(self.drawerOnClickButton(_:)), forControlEvents: .TouchUpInside)
       myScrollView.addSubview(button4)
       
       let button5 = UIButton()
       button5.frame = CGRectMake(self.view.frame.size.width / 3 * 4, 0, self.view.frame.size.width / 3, 100)
       button5.setTitle("ほげほげ5", forState: .Normal)
       button5.setTitleColor(UIColor.redColor(), forState: .Normal)
       button5.addTarget(self, action: #selector(self.drawerOnClickButton(_:)), forControlEvents: .TouchUpInside)
       myScrollView.addSubview(button5)
       
       myScrollView.contentSize = CGSizeMake(self.view.frame.size.width / 3 + self.view.frame.size.width / 3 + self.view.frame.size.width / 3 + self.view.frame.size.width / 3 + self.view.frame.size.width / 3, 100)
       myScrollView.pagingEnabled = true
       self.view.addSubview(myScrollView)