Post

Replies

Boosts

Views

Activity

Getting Error: Type '()' cannot conform to 'View'
can't see what the problem is .. Im getting the error: Type '()' cannot conform to 'View' struct CalendarView: View { @StateObject private var viewModel = CalendarViewModel() @State private var selectedDate: CalendarDate? @State private var showModal = false var body: some View { VStack { Text("Calendar App") .font(.largeTitle) .padding() GridStack(rows: 5, columns: 7) { row, col in let index = row * 7 + col if index < viewModel.calendarDates.count { let calendarDate = viewModel.calendarDates[index] Text("\(Calendar.current.component(.day, from: calendarDate.date))") .frame(width: 40, height: 40) .background(calendarDate.isSelected ? Color.blue : Color.clear) .cornerRadius(10) .foregroundColor(calendarDate.isSelected ? Color.white : Color.black) .onLongPressGesture { selectedDate = calendarDate showModal = true } } } } .background(Color.gray) .sheet(isPresented: $showModal) { if let date = selectedDate { DateSelectionModal(selectedDate: date) } } } } struct GridStack<Content: View>: View { let rows: Int let columns: Int let content: (Int, Int) -> Content var body: some View { VStack { ForEach(0..<rows) { row in HStack { ForEach(0..<columns) { column in content(row, column) } } } } } } #Preview { CalendarView() }
2
0
116
2w
How to select item or "date" in VGrid
HStack { ForEach(days, id:\.self) { day in Text(day) .font(.system(size: 12, weight: .medium)) .frame(maxWidth: .infinity) } } LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 7), spacing: 20) { ForEach(fetchDates()) { value in ZStack { if value.day != -1 { let hasAppts = manager.days.contains(value.date.monthDayYearFormat()) NavigationLink(value: AppRouter.day(date: value.date)) { Text("\(value.day)") .foregroundColor(hasAppts ? .blue : .black) .fontWeight(hasAppts ? .bold : .none) .background { ZStack(alignment: .bottom) { Circle() .frame(width: 48, height: 48) .foregroundColor(hasAppts ? .blue.opacity(0.1) : .clear) if value.date.monthDayYearFormat() == Date().monthDayYearFormat() { Circle() .frame(width: 8, height: 8) .foregroundColor(hasAppts ? .blue : .gray) } } } } .disabled(!hasAppts) } else { Text("") } } .frame(width: 32, height: 32) } } } .padding() }
0
0
268
Dec ’23
Scrollview not scrolling...
I embedded a vertical scrollview to a calendar that was originally horizontally navigation with buttons. But its isn't scrolling changing months. What am I missing? let days = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"] @State private var selectedMonth = 0 @State private var selectedDate = Date() @State private var path = NavigationPath() var body: some View { NavigationStack(path: $path) { ScrollView { VStack { Rectangle() .frame(height: 1) .foregroundColor(.gray) VStack(spacing: 20) { // Month Selection HStack { Spacer() Button { withAnimation { selectedMonth -= 1 } } label: { Image(systemName: "lessthan.circle.fill") .resizable() .scaledToFit() .frame(width: 32, height: 32) .foregroundColor(.gray) } Spacer() Text(selectedDate.monthYearFormat()) .font(.title2) Spacer() Button { withAnimation { selectedMonth += 1 } } label: { Image(systemName: "greaterthan.circle.fill") .resizable() .scaledToFit() .frame(width: 32, height: 32) .foregroundColor(.gray) } Spacer() } HStack { ForEach(days, id:\.self) { day in Text(day) .font(.system(size: 12, weight: .medium)) .frame(maxWidth: .infinity) } } LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 7), spacing: 20) { ForEach(fetchDates()) { value in ZStack { if value.day != -1 { let hasAppts = manager.days.contains(value.date.monthDayYearFormat()) NavigationLink(value: AppRouter.day(date: value.date)) { Text("\(value.day)") .foregroundColor(hasAppts ? .blue : .black) .fontWeight(hasAppts ? .bold : .none) .background { ZStack(alignment: .bottom) { Circle() .frame(width: 48, height: 48) .foregroundColor(hasAppts ? .blue.opacity(0.1) : .clear) if value.date.monthDayYearFormat() == Date().monthDayYearFormat() { Circle() .frame(width: 8, height: 8) .foregroundColor(hasAppts ? .blue : .gray) } } } } .disabled(!hasAppts) } else { Text("") } } .frame(width: 32, height: 32) } } } .padding() } } .navigationDestination(for: AppRouter.self) { router in switch router { case .day(let date): DayView(path: $path, currentDate: date) .environmentObject(manager) case .booking(let date): BookingView(path: $path, currentDate: date) .environmentObject(manager) case .confirmation(let date): ConfirmationView(path: $path, currentDate: date) } } .frame(maxHeight: .infinity, alignment: .top) .onChange(of: selectedMonth) { _ in selectedDate = fetchSelectedMonth() } } } func fetchDates() -> [CalendarDate] { let calendar = Calendar.current let currentMonth = fetchSelectedMonth() var dates = currentMonth.datesOfMonth().map({ CalendarDate(day: calendar.component(.day, from: $0), date: $0) }) let firstDayOfWeek = calendar.component(.weekday, from: dates.first?.date ?? Date()) for _ in 0..<firstDayOfWeek - 1 { dates.insert(CalendarDate(day: -1, date: Date()), at: 0) } return dates } func fetchSelectedMonth() -> Date { let calendar = Calendar.current let month = calendar.date(byAdding: .month, value: selectedMonth, to: Date()) return month! } }
0
0
262
Dec ’23
Help! with String Interpolation
Not sure what Im doing wrong here. Says "Cannot convert value of type 'String' to expected argument type 'Int'". let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "calCell", for: indexPath) as! CalendarCell cell.dayOfMonth.text = totalSquares[indexPath.item] //show the weekend days in different color switch indexPath.item { case 0,6,7,13,14,20,21,27,28,34,35,41: if Int(cell.dayOfMonth.text = "\(indexPath.item)" > 0 { cell.dayOfMonth.textColor = UIColor.lightGray } default: break } return cell }
4
0
668
Oct ’21
Xib file
I have a ViewController that calls a Xib view to popup (as a PressentationController). I need to put a single cell collectonview on that Xib view. How can I go about doing that because I cannot embed a collectionview in a Xib via storyboard. Is there a way to do that programmatically?
4
0
987
Oct ’21
collectionView and arrays (3 part question)
I have a collectionview of UILabels. when you tap a cell it should display its detail onto a pop up modal view. How do you display it onto that modal view in the same View controller? 2a. How can you cycle (loop) a group of arrays via UILabel forward and backwards in cells? 2b. How do you filter the arrays so that it always shows with a specific cell? (eg: Cell 2 will always have "D, E, F" when scrolling through) func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return totalSquares.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "calCell", for: indexPath) as! CalendarCell cell.dayOfMonth.text = totalSquares[indexPath.item] return cell } // Cell 1 Cell 2 Cell 3 let myArray: String = [[" A, B, C"], ["D, E, F"], ["G, H, I"]] etc..
4
0
705
Oct ’21
Long Press Gesture on collectionView not working
I've added long press programmatically but it does nothing. What am I doing wrong? Thanks in advance. import UIKit class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UIGestureRecognizerDelegate { @IBOutlet weak var monthLabel: UILabel! @IBOutlet weak var collectionView: UICollectionView! var selectedDate = Date() var totalSquares = [String]() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. setCellView() setMonthView() func setupLongGestureRecognizerOnCollection() { let longPressedGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(gestureRecognizer:))) longPressedGesture.minimumPressDuration = 0.5 longPressedGesture.delegate = self longPressedGesture.delaysTouchesBegan = true collectionView?.addGestureRecognizer(longPressedGesture) } } func setCellView() { let width = (collectionView.frame.size.width - 2) / 8 let height = (collectionView.frame.size.height - 2) / 8 let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout flowLayout.itemSize = CGSize(width: width, height: height) } func setMonthView(){ totalSquares.removeAll() let daysInMonth = CalendarHelper().daysInMonth(date: selectedDate) let firstDayOfMonth = CalendarHelper().firstOfMonth(date: selectedDate) let startingSpaces = CalendarHelper().weekDay(date: firstDayOfMonth) var count: Int = 1 while(count <= 42) { if(count <= startingSpaces || count - startingSpaces > daysInMonth) { totalSquares.append("") } else { totalSquares.append(String(count - startingSpaces)) } count += 1 } monthLabel.text = CalendarHelper().monthString(date: selectedDate) + " " + CalendarHelper().yearString(date: selectedDate) collectionView.reloadData() } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return totalSquares.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "calCell", for: indexPath) as! CalendarCell cell.dayOfMonth.text = totalSquares [indexPath.item] return cell } @objc func handleLongPress(gestureRecognizer: UILongPressGestureRecognizer) { if (gestureRecognizer.state != .began) { return } let p = gestureRecognizer.location(in: collectionView) if let indexPath = collectionView?.indexPathForItem(at: p) { print("Long press at item: \(indexPath.row)") } } @IBAction func previousMonthButton(_ sender: Any) { selectedDate = CalendarHelper().minusMonth(date: selectedDate) setMonthView() } @IBAction func nextMonthButton(_ sender: Any) { selectedDate = CalendarHelper().plusMonth(date: selectedDate) setMonthView() } override open var shouldAutorotate: Bool { return false } } The GitHub source is here: github.com/codeWithCal/CalendarExampleTutorial
2
0
2.5k
Oct ’21
How to extract data from URL/ HTML?
Im trying to extract the IP address that is nested in html and print it out to a string.&lt;ul&gt; &lt;li&gt;IP Address: &lt;span class="darktext"&gt;199.123.456.789&lt;/span&gt; &lt;/li&gt; &lt;li&gt;Internet Service Provider: &lt;span class="darktext"&gt;Optimum WiFi&lt;/span&gt; &lt;/li&gt; &lt;li&gt;City: &lt;span class="darktext"&gt;My City&lt;/span&gt; &lt;/li&gt; &lt;li&gt;State/Region: &lt;span class="darktext"&gt;New City&lt;/span&gt; &lt;/li&gt; &lt;li&gt;Country: &lt;span class="darktext"&gt;United States&lt;/span&gt; &lt;/li&gt; &lt;li&gt;Browser: &lt;span class="darktext"&gt;TextFromURL&lt;/span&gt; &lt;/li&gt; &lt;li&gt;Operating System: &lt;span class="darktext"&gt;&lt;/span&gt; &lt;/li&gt; &lt;li&gt;Screen Resolution: &lt;span id="resolution" class="darktext"&gt;&lt;/span&gt; &lt;/li&gt; &lt;/ul&gt;
1
0
834
May ’20
Im stuck/stumped - Array of UIView In a structure
Im trying to have a swipe func showing an array of images using a "For In Loop". The images and data that I want presented separately in a UIView for the images and and a few UILabels for the car information. Im getting stuck of how to extract the images "name" and putting them in the "for in" loop. Nothing seems to be working.import UIKit struct ExoticCars { let name: UIImage; let make: String; let model: String; let year: Int; let topSpeed: Double; let price: Int; class ViewController: UIViewController { @IBOutlet weak var imageVIew: UIImageView! @IBOutlet weak var makeLabel: UILabel! @IBOutlet weak var modelLabel: UILabel! @IBOutlet weak var yearLabel: UILabel! @IBOutlet weak var speedLabel: UILabel! @IBOutlet weak var priceLabel: UILabel! var index = 0 var exoticCar: [ExoticCars] = [ ExoticCars(name: imageLiteral(resourceName: "porsche_spyder"), make: "Porsche", model: "918 Spyder", year: 2015, topSpeed: 2.2, price: 931_975), ExoticCars(name: imageLiteral(resourceName: "tesla_p"), make: "Tesla", model: "Model S P100D", year: 2019, topSpeed: 2.28, price: 121_190), ExoticCars(name: imageLiteral(resourceName: "dodge_challenger"), make: "Dodge", model: "Challenger SRT", year: 2018, topSpeed: 2.3, price: 86_090), ExoticCars(name: imageLiteral(resourceName: "ferrari_laferrari"), make: "Ferrari", model: "LaFerrari", year: 2019, topSpeed: 2.4, price: 2_200_000), ExoticCars(name: imageLiteral(resourceName: "bugatti_chiron"), make: "Bugatti", model: "Chiron", year: 2019, topSpeed: 2.5, price: 3_000_000), ExoticCars(name: imageLiteral(resourceName: "rimac_concept1"), make: "Rimac", model: "Concept One", year: 2018, topSpeed: 1.85, price: 1_200_000), ExoticCars(name: imageLiteral(resourceName: "mclaren_720s"), make: "McLaren", model: "720s", year: 2019, topSpeed: 2.0, price: 299_000), ExoticCars(name: imageLiteral(resourceName: "porsche_gt2"), make: "Porsche", model: "911 GT2 RS", year: 2018, topSpeed: 2.6, price: 294_450), ExoticCars(name: imageLiteral(resourceName: "lamborghini_aventador"), make: "Lamborghini", model: "Aventador LP", year: 2016, topSpeed: 2.7, price: 497_895), ExoticCars(name: imageLiteral(resourceName: "koenigsegg"), make: "Koenigsegg", model: "Regera", year: 2017, topSpeed: 2.8, price: 2_000_000), ]; override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. var name = [UIView]() for name in exoticCar { } } @IBAction func swipeLeft(_ sender: UISwipeGestureRecognizer) { if name &lt; exoticCar.count - 1 { index = index + 1 } } @IBAction func swipeRight(_ sender: UISwipeGestureRecognizer) { if index &lt; exoticCar.count + 1 { index = index - 1 } } } }
2
0
707
Apr ’20
Crashes on Long Press
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(gesture:))) calendarView.addGestureRecognizer(longPressGesture) } @objc func handleLongPress(gesture: UILongPressGestureRecognizer) { guard gesture.state == .began else {return} let position = gesture.location(in: collectionView) if let indexPath = collectionView.indexPathForItem(at: position) { guard let modalCalVC = self.storyboard?.instantiateViewController(identifier: "ModalCalViewController") as? ModalCalViewController else { print("ModalCalViewController cannot be instantiated") return } let value = true //Get the value you want to pass from `indexPath` modalCalVC.passedValue = value present(modalCalVC, animated: true, completion: nil) } else { print("Long press not on a cell") } }I dont understand why it crashes I get Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file.swift, Line 10
1
0
684
Mar ’20
**Updated** How to pass data/value from View To View Controller?
I have a collection view (Calendar) and I have my modal view and I want the modal view to display what is in the cell when its selected.Im not sure how do I go about doing this?So far I changed the color of the cells background when selected.How do I call and animate the child view controller?and then display that cells content?// Reuseable Cell setupViews() myCollectionView.delegate=self myCollectionView.dataSource=self myCollectionView.register(dateCVCell.self, forCellWithReuseIdentifier: "Cell") }// cell selected BG color change func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let cell=collectionView.cellForItem(at: indexPath) cell?.backgroundColor=Colors.blue let lbl = cell?.subviews[1] as! UILabel lbl.textColor=UIColor.white }
9
0
2.8k
Feb ’20