Example of calling swift code from RN
This commit is contained in:
parent
1a9884e0e9
commit
151ed6d78b
12 changed files with 86 additions and 6 deletions
18
ios/Native/Application/Views/BaseButton/BaseButton.swift
Normal file
18
ios/Native/Application/Views/BaseButton/BaseButton.swift
Normal file
|
@ -0,0 +1,18 @@
|
|||
//
|
||||
// BaseButton.swift
|
||||
// RNPlayground
|
||||
//
|
||||
// Created by Artur Gurgul on 02/08/2025.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct BaseButton: View {
|
||||
@EnvironmentObject var sharedState: SharedState
|
||||
|
||||
var body: some View {
|
||||
Button("SwiftUI Button") {
|
||||
sharedState.message = "Clicked in SwiftUI button that was created in RN"
|
||||
}
|
||||
}
|
||||
}
|
12
ios/Native/Application/Views/BaseButton/BaseButtonManager.m
Normal file
12
ios/Native/Application/Views/BaseButton/BaseButtonManager.m
Normal file
|
@ -0,0 +1,12 @@
|
|||
//
|
||||
// BaseButtonManager.m
|
||||
// RNPlayground
|
||||
//
|
||||
// Created by Artur Gurgul on 02/08/2025.
|
||||
//
|
||||
|
||||
#import <React/RCTViewManager.h>
|
||||
|
||||
@interface RCT_EXTERN_MODULE(BaseButtonManager, RCTViewManager)
|
||||
|
||||
@end
|
|
@ -0,0 +1,23 @@
|
|||
//
|
||||
// BaseButtonManager.swift
|
||||
// RNPlayground
|
||||
//
|
||||
// Created by Artur Gurgul on 02/08/2025.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import React
|
||||
import SwiftUI
|
||||
|
||||
@objc(BaseButtonManager)
|
||||
class BaseButtonManager: RCTViewManager {
|
||||
override func view() -> UIView! {
|
||||
return UIHostingController(
|
||||
rootView: BaseButton()
|
||||
).view
|
||||
}
|
||||
|
||||
override static func requiresMainQueueSetup() -> Bool {
|
||||
return true
|
||||
}
|
||||
}
|
60
ios/Native/Application/Views/CustomButton/CustomButton.swift
Normal file
60
ios/Native/Application/Views/CustomButton/CustomButton.swift
Normal file
|
@ -0,0 +1,60 @@
|
|||
//
|
||||
// CustomButton.swift
|
||||
// RNPlayground
|
||||
//
|
||||
// Created by Artur Gurgul on 02/08/2025.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import SwiftUI
|
||||
|
||||
|
||||
@objc(CustomButton)
|
||||
class CustomButton: UIView {
|
||||
|
||||
private let label: UILabel = {
|
||||
let lbl = UILabel()
|
||||
lbl.text = "Hello from native"
|
||||
lbl.textColor = .white
|
||||
lbl.translatesAutoresizingMaskIntoConstraints = false
|
||||
return lbl
|
||||
}()
|
||||
|
||||
private let button: UIButton = {
|
||||
let btn = UIButton(type: .system)
|
||||
btn.setTitle("Click Me", for: .normal)
|
||||
btn.setTitleColor(.white, for: .normal)
|
||||
btn.translatesAutoresizingMaskIntoConstraints = false
|
||||
return btn
|
||||
}()
|
||||
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
self.backgroundColor = .systemBlue
|
||||
|
||||
self.addSubview(label)
|
||||
self.addSubview(button)
|
||||
|
||||
setupConstraints()
|
||||
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
private func setupConstraints() {
|
||||
NSLayoutConstraint.activate([
|
||||
label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 16),
|
||||
label.centerYAnchor.constraint(equalTo: self.centerYAnchor),
|
||||
|
||||
button.leadingAnchor.constraint(equalTo: label.trailingAnchor, constant: 12),
|
||||
button.centerYAnchor.constraint(equalTo: self.centerYAnchor),
|
||||
button.trailingAnchor.constraint(lessThanOrEqualTo: self.trailingAnchor, constant: -16)
|
||||
])
|
||||
}
|
||||
|
||||
@objc private func buttonTapped() {
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
//
|
||||
// CustomButtonManager.m
|
||||
// RNPlayground
|
||||
//
|
||||
// Created by Artur Gurgul on 02/08/2025.
|
||||
//
|
||||
|
||||
#import <React/RCTViewManager.h>
|
||||
|
||||
@interface RCT_EXTERN_MODULE(CustomButtonManager, RCTViewManager)
|
||||
|
||||
@end
|
|
@ -0,0 +1,20 @@
|
|||
//
|
||||
// ViewManager.swift
|
||||
// RNPlayground
|
||||
//
|
||||
// Created by Artur Gurgul on 02/08/2025.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import React
|
||||
|
||||
@objc(CustomButtonManager)
|
||||
class CustomButtonManager: RCTViewManager {
|
||||
override func view() -> UIView! {
|
||||
return CustomButton()
|
||||
}
|
||||
|
||||
override static func requiresMainQueueSetup() -> Bool {
|
||||
return true
|
||||
}
|
||||
}
|
|
@ -18,7 +18,7 @@ struct ToolboxHeader: View {
|
|||
sharedState.send(message: "hello from Swift!")
|
||||
}
|
||||
}
|
||||
Text("SwiftUI => RN => SwiftUI: \(sharedState.text1)")
|
||||
Text("Message: \(sharedState.message)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue