avdlee / swiftuikitview Goto Github PK
View Code? Open in Web Editor NEWEasily use UIKit views in your SwiftUI applications. Create Xcode Previews for UIView elements
Home Page: https://www.avanderlee.com
License: MIT License
Easily use UIKit views in your SwiftUI applications. Create Xcode Previews for UIView elements
Home Page: https://www.avanderlee.com
License: MIT License
struct ContentView: View {
@State var counter = 0
var body: some View {
VStack {
Text("Hello no \(counter) from SwiftUI")
.padding()
UILabel() // <- This can be any `UIKit` view.
.swiftUIView(layout: .intrinsic) // <- This is returning a SwiftUI `View`.
.set(\.text, to: "Hello no \(self.counter) from UIKit") // <- Use key paths for updates.
.fixedSize()
}
.onAppear {
if counter == 0 {
schedule()
}
}
}
func schedule() {
counter += 1
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.schedule()
}
}
}
I expected the UILabel to be updated and behave like my SwiftUI Text-view, but instead it is showing just 0. Also, recreating the UILabel every time the view hierarchy is declared is not a good idea as it may happen many many times. I think this is a fundamental issue with the approach in this library. I find this approach better and easier
import SwiftUI
import UIKit
struct UIViewMaker<ViewType: UIView>: UIViewRepresentable {
typealias UIViewType = ViewType
var make: () -> ViewType = ViewType.init
var update: (ViewType) -> ()
func makeUIView(context: Context) -> ViewType {
return make()
}
func updateUIView(_ uiView: ViewType, context: Context) {
update(uiView)
}
}
struct ContentView: View {
@State var counter = 0
var body: some View {
VStack {
Text("Hello no \(counter) from SwiftUI")
.padding()
UIViewMaker<UILabel> {
$0.text = "Hello no \(self.counter) from UIKit"
}
.fixedSize()
}
.onAppear {
if counter == 0 {
schedule()
}
}
}
func schedule() {
counter += 1
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.schedule()
}
}
}
UILabel
Preview Example in project SwiftUIKitExample.xcodeproj
does not work.
UILabel() // <- This is a `UIKit` view.
.swiftUIView(layout: .intrinsic) // <- This is a SwiftUI `View`.
.set(\.text, to: "Hello, UIKit!") // <- Use key paths for updates.
.fixedSize() // <- Make sure the size is set
.previewLayout(.sizeThatFits)
.previewDisplayName("UILabel Preview Example")
}
Expected to see the text "Hello, UIKit!" but that text is not visible in the preview :(
Tested with Xcode 14.0.1 and iPhone 13 (iOS 16) simulator
When I try to compile for macOs I get: No such module 'SwiftUIKitView'
xcode 12.5.1
macos 11.6
Hi, I have tried the library it is very perfectly if I am adding an UILabel or UIView. But now I am trying to add UIActivityIndicatorView, seems it doesn't work. Is this syntax below correct ?. I tried this but the view is not showing anything, but once I try with UILabel or UIView, it works fine.
UIActivityIndicatorView(style: .large)
.swiftUIView(layout: .fixed(size: CGSize(width: 100, height: 100)))
.set(\.color, to: UIColor.blue)
.set(\.isHidden, to: false)
.fixedSize()
second question, how did I trigger a function that preserved from one of the subclass of UIView ? Let say this activity indicator has an open function called as startAnimating()
. however I can't using the declarative syntax in above code, is it expected to only set a value but can't call any derived method ?
Thank you before, @AvdLee
public struct ContentView: View {
var body: some View {
return AnyView(
ViewController().view
.swiftUIView(layout: .intrinsic)
)
}
}
class ViewController: UIViewController {
var scrollView = UIScrollView()
let contentView = UIView()
var pullControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
setupScrollView()
setupViews()
}
@objc func handleRefreshControl() {
// Update your content…
print("hi")
// Dismiss the refresh control.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.scrollView.refreshControl?.endRefreshing()
}
}
func setupScrollView(){
scrollView.translatesAutoresizingMaskIntoConstraints = false
contentView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
scrollView.addSubview(contentView)
scrollView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
scrollView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
scrollView.backgroundColor = UIColor.red
scrollView.refreshControl = pullControl
scrollView.refreshControl?.addTarget(self, action:
#selector(handleRefreshControl),
for: .valueChanged)
contentView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
contentView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
}
func setupViews(){
child.view.sizeToFit()
child.view.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(child.view)
child.view.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
child.view.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
child.view.widthAnchor.constraint(equalTo: contentView.widthAnchor).isActive = true
child.view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}
let child: UIHostingController = UIHostingController(rootView: TestView())
}
public struct TestView : View {
public var body: some View {
VStack {
Group {
Text("Test1")
Text("Test2")
Text("Test3")
Text("Test4")
Text("Test5")
Text("Test6")
Text("Test7")
Text("Test8")
Text("Test9")
Text("Test10")
}
}
}
}
When I swipe to refresh, refreshData isn't being called. The refresh animation plays endlessly. However, my code works when it isn't embedded in a SwiftUI view. Is this issue fixable?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.