Skip to content

Instantly share code, notes, and snippets.

@Mercandj
Last active January 7, 2023 17:01
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 Mercandj/a230d0583f8d98cd3537083584dcac3b to your computer and use it in GitHub Desktop.
Save Mercandj/a230d0583f8d98cd3537083584dcac3b to your computer and use it in GitHub Desktop.
swiftui_preview_with_ui_view
import UIKit
class HelloWorldView: UIView {
private lazy var title = createTitle()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(title)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
title.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
title.centerXAnchor.constraint(equalTo: self.centerXAnchor),
title.centerYAnchor.constraint(equalTo: self.centerYAnchor),
title.widthAnchor.constraint(equalTo: self.widthAnchor),
title.heightAnchor.constraint(equalTo: self.heightAnchor)
])
}
private func createTitle() -> UILabel {
let view = UILabel(frame: CGRect())
view.text = "Hello World"
return view
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment