Created
June 16, 2020 08:33
-
-
Save zafarivaev/710859161d360cb8cfcf6f1805a97077 to your computer and use it in GitHub Desktop.
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
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