Created
October 3, 2021 12:30
-
-
Save SatoTakeshiX/1b9050a7ea517785e3b45ace0a38a5ac to your computer and use it in GitHub Desktop.
Order by CALayer and UIView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// CALayerFrame | |
// | |
// Created by satoutakeshi on 2021/10/02. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
layoutSignboard() | |
} | |
func layoutSignboard() { | |
let signboard = Signboard(frame: .init(x: 0, y: 0, width: 100, height: 100)) | |
signboard.translatesAutoresizingMaskIntoConstraints = false | |
view.addSubview(signboard) | |
signboard.heightAnchor.constraint(equalToConstant: 200).isActive = true | |
signboard.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16).isActive = true | |
signboard.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16).isActive = true | |
signboard.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true | |
} | |
} | |
class Signboard: UIView { | |
init() { | |
super.init(frame: .zero) | |
setup() | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
setup() | |
} | |
required init?(coder: NSCoder) { | |
super.init(coder: coder) | |
setup() | |
} | |
private func setup() { | |
addSubview(boardView) | |
boardView.topAnchor.constraint(equalTo: topAnchor).isActive = true | |
boardView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true | |
boardView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true | |
boardView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true | |
boardView.addSubview(titleLabel) | |
titleLabel.topAnchor.constraint(equalTo: boardView.topAnchor, constant: 16).isActive = true | |
titleLabel.leadingAnchor.constraint(equalTo: boardView.leadingAnchor, constant: 16).isActive = true | |
titleLabel.trailingAnchor.constraint(equalTo: boardView.trailingAnchor, constant: -16).isActive = true | |
titleLabel.bottomAnchor.constraint(equalTo: boardView.bottomAnchor, constant: -16).isActive = true | |
boardView.layer.addSublayer(borderLayer) | |
boardView.didLayoutSubView = { [weak self] bounds in | |
self?.borderLayer.frame = bounds | |
} | |
boardView.addSubview(crownImageView) | |
crownImageView.topAnchor.constraint(equalTo: boardView.topAnchor, constant: -20).isActive = true | |
crownImageView.centerXAnchor.constraint(equalTo: boardView.centerXAnchor).isActive = true | |
} | |
private lazy var boardView: BoardView = { | |
let view = BoardView() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
// view.layer.borderWidth = 2 | |
// view.layer.borderColor = UIColor.systemGreen.cgColor | |
// view.layer.cornerRadius = 4 | |
return view | |
}() | |
private lazy var borderLayer: CALayer = { | |
let layer = CALayer() | |
layer.borderWidth = 2 | |
layer.borderColor = UIColor.systemGreen.cgColor | |
layer.cornerRadius = 4 | |
return layer | |
}() | |
private lazy var crownImageView: UIImageView = { | |
let imageView = UIImageView(image: .init(systemName: "crown")) | |
imageView.translatesAutoresizingMaskIntoConstraints = false | |
imageView.contentMode = .scaleAspectFit | |
imageView.widthAnchor.constraint(equalToConstant: 40).isActive = true | |
imageView.heightAnchor.constraint(equalToConstant: 40).isActive = true | |
imageView.backgroundColor = .systemRed | |
return imageView | |
}() | |
private lazy var titleLabel: UILabel = { | |
let label = UILabel() | |
label.translatesAutoresizingMaskIntoConstraints = false | |
label.text = "ランキング" | |
label.numberOfLines = 0 | |
label.font = UIFont.preferredFont(forTextStyle: .title1) | |
label.adjustsFontForContentSizeCategory = true | |
label.textAlignment = .center | |
return label | |
}() | |
} | |
final class BoardView: UIView { | |
var didLayoutSubView: ((CGRect) -> Void)? | |
init() { | |
super.init(frame: .zero) | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
didLayoutSubView?(bounds) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://blog.personal-factory.com/2021/10/03/order-by-calayer-and-uiview/