[SWIFT] Implement CustomView in code

Introduction

When I implemented CustomView in code, my understanding of the init method was ambiguous, so I will post it as a reminder.

Operating environment

【Xcode】Version 12.0.1 【Swift】Version 5.3

Implementation code

CustomView.swift


import UIKit

class CustomView: UIView {
    
    var imageView: UIImageView!
    
    //Detailed explanation ①
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    //Detailed explanation ②
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
    
    private func setup(){
        let iWidth: CGFloat = 200
        let iHeight: CGFloat = 200
        let posX: CGFloat = (self.frame.width - iWidth)/2
        let posY: CGFloat = (self.frame.height - iHeight)/2
        
        imageView = UIImageView(frame: CGRect(x: posX, y: posY, width: iWidth, height: iHeight))
        imageView.image = UIImage(named: "hoge")
        self.addSubview(imageView)
    }
    
}

ViewController.swift


import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let customView = CustomView()
        customView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
        self.view.addSubview(customView)
    }
}

Detailed explanation ①

override init(frame: CGRect) {
    super.init(frame: frame)
}

Detailed explanation ②

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

reference

Recommended Posts

Implement CustomView in code
[Beginner] Implement NavigationBar in code
Implement Swift UITextField in code
Implement markdown in Rails
How to implement UICollectionView in Swift with code only
Implement application function in Rails
Implement follow function in Rails
Code entry method in Qiita
Implement two-step verification in Java
Implement LTI authentication in Rails
Implement Basic authentication in Java
Implement math combinations in Java
2 Implement simple parsing in Java
Java in Visual Studio Code
[Android] Implement Adblock in WebView
Implement Email Sending in Java
Write Java8-like code in Java8
Implement import process in Rails
Implement functional quicksort in Java
Implement rm -rf in Java.
Implement XML signature in Java
Implement Table Driven Test in Java 14
Guess the character code in Java
3 Implement a simple interpreter in Java
Try to implement Yubaba in Ruby
Java Spring environment in vs Code
Write test code in Spring Boot
Implement simple login function in Rails
Implement tagging function in form object
Implement reCAPTCHA v3 in Java / Spring
Implement Material Design View in Kotlin
Implement PHP implode function in Java
Implement a gRPC client in Ruby
Implement REST API in Spring Boot
Implement a contact form in Rails
Implement Spring Boot application in Gradle
Try to implement Yubaba in Java
Implement CSV download function in Rails
1 Implement simple lexical analysis in Java
The application absorbs the difference in character code
How to implement search functionality in Rails
Implement FizzBuzz problem in test-driven development (preparation)
How to implement date calculation in Java
Using Hystrix and Sentinel in code example
Try to implement n-ary addition in Java
[HTTP] Status code included in the HTTP response
Implement the algorithm in Ruby: Day 1 -Euclidean algorithm-
Enable code completion in Eclipse for Mac
Implement FizzBuzz problems in test-driven development (practice)
How to implement coding conventions in Java
All same hash code string in Java
Implement something like a stack in Java
How to implement ranking functionality in Rails
How to implement asynchronous processing in Outsystems
[Mac] Install Java in Visual Studio Code
Implement button transitions using link_to in Rails