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?
Post
Replies
Boosts
Views
Activity
It's about importing the package dependencies Snapkit. The Add Packages added it correctly as per screenshot, but if I want to reference the chart like this..
`// Line Chart
LineChartView(data: clickData, title: "Clicks per Day", legend: "Daily Clicks")
.padding()
It got me some error
/Main View/AffiliateViewController.swift:59:17 Cannot find 'LineChartView' in scope `
Here is how the Snapkit sits in XCode. Do I need to add something more like dependencies so that Xcode does not flag it as cannot find?
I have an xCode project called Vision + CoreML
I wanted add some charts into it so I started installing cocoapad for the first time
I followed all the steps from installing the latest ruby version to running pod install
The critical .xcworkspace had been never created no matter how many times pod install was run. It is nowhere to be found. Could anyone advise me on what went wrong here?
Here are some files used to generate
Podfile
platform :ios, '15.0'
target 'Vision + CoreML' do
use_frameworks!
# Comment the next line if you don't want to use dynamic frameworks
pod 'Alamofire', '~> 5.6'
# Pods for Vision + CoreML
pod 'Charts'
end
and here is the command prompt output ls
MacBook-Pro-3:NotAbgabe myusername$ ls
App Main View
Configuration Models
Documentation Podfile
Extensions README.md
Image Predictor Vision+Core-ML.xcodeproj
LICENSE
I ran all the commands under the NotAbgabe folder. Not sure if xcworkspace is hidden somewhere between the files
I am very new to all this. What I wanted:
I wanted this textfield (where there is "I am the placeholder") to allow users to type any string:
At the moment this textfield does not let me type in anything. It sits like a stone. There are no corresponding errors to this but this behaviour is not my liking. I wanted it to let me just type string.
Could anyone kindly point me in the right direction?
This is how IBOutlet connection made to the TextField in question
And the code snippet
class ViewController: UIViewController {
@IBOutlet weak var companyTextField2: UITextField!
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
private var models = [Consulting]()
//more other codes
@IBAction func insertName(_ sender: UITextField) {
// Create the UITextField
let nameTextField = UITextField()
nameTextField.placeholder = "Enter Name"
// Option 1: Add the UITextField to the view (if needed)
view.addSubview(nameTextField)
// Option 2: Use the UITextField (e.g., get or set its text)
let enteredName = nameTextField.text
print("Entered Name:", enteredName ?? "")
}
}