目的
iOSアプリでカスタムViewを轟速で作るためのメモです。
超速 → 爆速 → 轟速
爆速よりも速い、轟速で作りたいと思います。
環境
- Xcode 11
- iOS12.4
カスタムView
まず、カスタムView用のswiftファイルを作製します。
CustomView.swift
として、以下のように作成しました。
import UIKit
class CustomView: UIView {
@IBOutlet weak var label: UILabel!
override init(frame: CGRect){
super.init(frame: frame)
initView()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
initView()
}
func initView(){
}
func labelModify() {
label.text = "aiueo"
}
@IBAction func actionCloseView(_ sender: Any) {
self.removeFromSuperview()
}
}
CustomViewのレイアウトは、Xibファイルとして、以下のようにしました。
viewとファイルを直接つなげます。
ちなみに、File’s Ownerは特に何もしていません。
カスタムViewの呼び出し方
ViewControllerなどから呼び出す方法は以下です。
let customView = Bundle.main.loadNibNamed("CustomView", owner: self, options: nil)?.first as! CustomView
customView.frame = CGRect(x: 50, y: 200, width: self.view.frame.width - 100, height: self.view.frame.height - 400)
self.view.addSubview(customView)
customView.labelModify()
今回はボタンをタップしたら上記を呼び出すようにしました。
挙動
動きは以下です。
轟速で作れました。(^o^)
メモは以上です。