Last active
January 7, 2023 17:01
-
-
Save Mercandj/a230d0583f8d98cd3537083584dcac3b to your computer and use it in GitHub Desktop.
swiftui_preview_with_ui_view
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 | |
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