Skip to content

Instantly share code, notes, and snippets.

@zafarivaev
Created June 16, 2020 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zafarivaev/710859161d360cb8cfcf6f1805a97077 to your computer and use it in GitHub Desktop.
Save zafarivaev/710859161d360cb8cfcf6f1805a97077 to your computer and use it in GitHub Desktop.
import UIKit
import PlaygroundSupport
class CustomView: UIView {
var message: String = "" {
willSet {
print("newValue: \(newValue)")
updateUI(with: newValue)
}
}
func updateUI(with message: String) {
messageLabel.text = "Message: \(message)"
}
lazy var messageLabel: UILabel = {
let label = UILabel()
label.textColor = .black
label.text = "Message: \(message)"
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .white
self.addSubview(messageLabel)
NSLayoutConstraint.activate([
messageLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor),
messageLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor),
])
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.message = "Hello World!"
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
let view = CustomView(
frame: CGRect(x: 0,
y: 0,
width: 300,
height: 300)
)
PlaygroundPage.current.liveView = view
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment