The problem is:
As per screenshot below, one can only see the lineChart. I have another struct AffiliateView coded under this Chart:
import SnapKit
import Charts
import DGCharts
class AffiliateViewController: UIViewController {
private lazy var chartView: LineChartView = {
let chart = LineChartView()
chart.noDataText = "No data available."
chart.chartDescription.enabled = false
chart.xAxis.labelPosition = .bottom
chart.rightAxis.enabled = false
chart.legend.enabled = true
chart.backgroundColor = .lightGray // For debugging visibility
return chart
}()
private lazy var containerView: UIView = {
let view = UIView()
view.backgroundColor = .white
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
// Add container view and chart view to the main view
view.addSubview(containerView)
view.addSubview(chartView)
// Add SwiftUI View inside the container view
let affiliateView = AffiliateView()
let hostingController = UIHostingController(rootView: affiliateView)
addChild(hostingController)
containerView.addSubview(hostingController.view)
hostingController.view.frame = containerView.bounds
hostingController.didMove(toParent: self)
layout()
setupChartData()
}
private func layout() {
// Layout the container view (SwiftUI content)
containerView.snp.makeConstraints { make in
make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
make.left.right.equalToSuperview()
make.height.equalTo(350) // Increase the height for the SwiftUI content
}
// Layout the chart view below the container view
chartView.snp.makeConstraints { make in
make.top.equalTo(containerView.snp.bottom).offset(20) // Space between chart and the affiliate content
make.left.equalToSuperview().offset(20)
make.right.equalToSuperview().offset(-20)
make.height.equalTo(200) // Set a fixed height for the chart
}
}
private func setupChartData() {
let dataEntries = [
ChartDataEntry(x: 1, y: 10),
ChartDataEntry(x: 2, y: 20),
ChartDataEntry(x: 3, y: 15),
ChartDataEntry(x: 4, y: 30),
ChartDataEntry(x: 5, y: 25)
]
let dataSet = LineChartDataSet(entries: dataEntries, label: "Clicks per Day")
dataSet.colors = [.blue]
dataSet.valueColors = [.black]
dataSet.circleColors = [.red]
dataSet.circleRadius = 4.0
let data = LineChartData(dataSet: dataSet)
chartView.data = data
chartView.notifyDataSetChanged()
}
}
// SwiftUI View remains in the same file
struct AffiliateView: View {
@State private var customMessage: String = ""
@State private var uniqueLink: String = "Your unique link will appear here."
@State private var clickData: [Double] = [10, 20, 15, 30, 25] // Example data
var body: some View {
NavigationView {
VStack(spacing: 20) {
// TextField for custom message input
TextField("Enter your custom message...", text: $customMessage)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding(.horizontal)
// Generate Link Button
Button(action: generateLink) {
Text("Generate Sign-Up Link")
.font(.headline)
.foregroundColor(.white)
.frame(maxWidth: .infinity, maxHeight: 50)
.background(Color.red)
.cornerRadius(10)
}
.padding(.horizontal)
// Generated Link Label
Text(uniqueLink)
.font(.body)
.multilineTextAlignment(.center)
.padding(.horizontal)
// You can add a chart here if you want to show it in SwiftUI too
/* LineChartView(data: clickData, title: "Clicks per Day", legend: "Daily Clicks") */
}
.navigationTitle("Affiliate Marketing")
.navigationBarTitleDisplayMode(.inline)
}
}
private func generateLink() {
let encodedMessage = customMessage.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
uniqueLink = "https://affiliate.example.com/referral?message=\(encodedMessage)"
addClickData()
}
private func addClickData() {
clickData.append(Double.random(in: 0...100))
}
}
As you see, the AffiliateView has been declared outside of Controller View class. The View content was visible before the lineChart was added into this code. Now the View content is not visible anymore. I have tried to increment/decrement values at make.height.equalTo() but to no avail.
Could anyone kindly point me in the right direction?