How to use Swift UIScrollView

Implement UIScrollView in code

I'm self-taught to make an app, but by the way, I thought I had never used scrollView, so I studied! First, I tried using scrollView without looking at it at all. Here is the code

class ViewController: UIViewController {
    
    private let label = UILabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let scrollView = UIScrollView()
        
        scrollView.frame = .init(x: 0, y: 0, 
            width: view.frame.size.width, height: view.frame.size.height)
        
        view.addSubview(scrollView)
        scrollView.addSubview(label)
        
        label.text = "hello world"
        label.frame = .init(x: 0, y: 0, width: 100, height: 50)
        label.center = scrollView.center
    }
    
}

The result could not be scrolled. label is added to make sure that scrollView is displayed properly.

why! ?? It seems that you have to specify something called contentSize. contentSize seems to determine the scroll area. Here is the corrected one

class ViewController: UIViewController {
    
    private let label = UILabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let scrollView = UIScrollView()
        
        //Set the size of scrollView.
        scrollView.frame = .init(x: 0, y: 0, 
            width: view.frame.size.width * 2, height: view.frame.size.height * 2)
        
        //Scroll area settings
        scrollView.contentSize = CGSize(width:view.frame.size.width * 2, height:view.frame.size.height * 2)
        
        //Add scrollView as SubView of view
        view.addSubview(scrollView)
        scrollView.addSubview(label)
        
        label.text = "hello world"
        label.frame = .init(x: 0, y: 0, width: 100, height: 50)
        label.center = scrollView.center
    }
    
}

How about this! I built it, but I couldn't scroll.

Upon further investigation, it seems that frame must be smaller than contentSize.

class ViewController: UIViewController {
    
    private let label = UILabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let scrollView = UIScrollView()
        
        //Set the size of scrollView.
        scrollView.frame = .init(x: 0, y: 0, 
            width: view.frame.size.width, height: view.frame.size.height)
        
        //Scroll area settings
        scrollView.contentSize = CGSize(width:view.frame.size.width * 2, height:view.frame.size.height * 2)
        
        //Add scrollView as SubView of view
        view.addSubview(scrollView)
        scrollView.addSubview(label)
        
        label.text = "hello world"
        label.frame = .init(x: 0, y: 0, width: 100, height: 50)
        label.center = scrollView.center
    }
    
}

The result is ... I got it! !! !!

Summary

When using UIScrollView

contentSize must be specified contentSize must be larger than frame

Please note that and implement it!

Quote

[Swift4] How to use scroll view

Recommended Posts

How to use Swift UIScrollView
[Swift] How to use UserDefaults
[Swift] How to use SwiftLint (cocoapods)
[Swift] How to use Unwind segue
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use collection_select
[Swift] How to use Tab Bar Controller
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
[Swift] How to use one option alert
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
Note how to use Swift super basic TableView
[Java] How to use Map
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
[Rails] How to use enum
How to use java class
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
How to use GC Viewer
[Java] How to use Optional ①
How to use Lombok now
[Creating] How to use JUnit
[Rails] How to use Scope
How to use the link_to method
[Rails] How to use gem "devise"
How to use Lombok in Spring