Hello, as discussed in my previous topic with @Claude31 (Link ) I have a problem with riding off layout. Should I register the nib somewhere and in nib put constraints for a cell? Below I'm showing What I've got so far and how it looks like on device:
I have tried many solution but nothing helped, so i just putted constraints for collection view. I dont know how to put constraints for the cell (It is greyd out), maybe in nib file?:
I have defined there class and identifier.
Here is how it looks like on device:
I would like it to look like in the first screen shot (storyboard).
Here are my files connected with collectionview vc:
First one is Controller with code, another two are connected with the cell.
Here is the ModernizmCollectionViewCell file:
import UIKit
class ModernizmCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageCell: UIImageView!
@IBOutlet weak var navButton: UIIButton!
@IBOutlet weak var title: UILabel!
@IBOutlet weak var subTitle: UILabel!
@IBOutlet weak var locationLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
Many thank for your help.
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hello,
I try to fetch data from my API every 5 seconds. I did this code, but it does not work. Any ideas what I'm doing wrong?
let urlString = "https://radio.app/api/nowplaying/radio_x"
let url = URL(string: urlString)!
let session = URLSession.shared
let dataTask = session.dataTask(with: url) { data, response, error in
if let error = error {
print(error)
return
}
guard let data = data else {
print("data is nil")
return
}
let decoder = JSONDecoder()
do {
let radio = try decoder.decode(RadioAPI.self, from: data)
print(radio)
DispatchQueue.main.asyncAfter(deadline: .now() + 5, execute: {
//this is the line that I've added DispatchQueue.main.asyncDispatchQueue.main.asyncAfter insted of standard
self.songtitle.text = radio.nowPlaying.song.title
self.artist.text = radio.nowPlaying.song.artist
//albumcover art section
if let artUrl = URL(string: radio.nowPlaying.song.art) {
//I need to load an image from `artUrl`
let imageDatatask = session.dataTask(with: artUrl) { imageData, imageResponse, imageError in
if let error = error {
print(error)
return
}
guard let imageData = imageData else {
print("image_data is nil")
return
}
//let albumArt = UIImage(data: imageData)
DispatchQueue.main.async {
let albumArt = UIImage(data: imageData)
let albumView = UIImageView(image: albumArt)
}
}
imageDatatask.resume()
}
})
}
catch {
print("Error Parsing JSON: \(error)")
}
}
dataTask.resume()
}
Hello,
This is the continuation of https://developer.apple.com/forums/thread/692878?login=true&page=1#692455022 where @Calude31 tried to help me. I've added two buttons for my annotationView. I have 25 annotations and I would like to have seperate button actions for each annotation. Now when I try to customize an action per annotation for rightButton switch only changes colors of the annotations NOT the rightButton.addTarget(self, action: #selector(didClickDetailDisclosure(button:)), for: .touchUpInside) - I think because od the return annotationView at the end - I need to change all three annotation parameters (color, icon, button action). Here is my code:
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
let initialLocation = CLLocation(latitude: 85.10786543576327, longitude: 11.03851472106171)
let locationManager = CLLocationManager()
struct Place {
let name: String
let description: String
let type: String
let lattitude: CLLocationDegrees
let longtitude: CLLocationDegrees
var coordinate: CLLocationCoordinate2D {
.init(latitude: lattitude, longitude: longtitude)
}
}
let location = CLLocation()
let places = [Place(name: "One", description: "One", type: "one", lattitude: 81.108187, longtitude: 12.075812),
Place(name: "Two", description: "Two", typ: "two", lattitude: 81.076187, longtitude: 11.000563),
Place(name: "Three", description: "Three", typ: "Three", lattitude: 81.076187, longtitude: 11.000563)]
[...]
func findPlace(_ miejsca: [Place]) {
for (i, place) in places.enumerated() {
let annotations = MKPointAnnotation()
annotations.title = String(i) + "-" + place.name
annotations.subtitle = place.description
annotations.coordinate = CLLocationCoordinate2D(latitude:
place.lattitude, longitude: place.longtitude)
mapView.addAnnotation(annotations)
}
}
@objc func didClickDetailDisclosure(button: UIButton) {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "kopuly_vc") as? KopulyController else {
return
}
present(vc, animated: true)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !(annotation is MKUserLocation) else { return nil }
mapView.delegate = self
let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: String(annotation.hash))
let identifier = "identifier"
annotationView.canShowCallout = true
guard let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier, for: annotation) as? MKMarkerAnnotationView else { return nil }
let rightButton = UIButton(type: .detailDisclosure) // HERE IS THIS BUTTON, BUT I NEED EACH BUTTON FOR EACH ANNOTATION - THIS WAY IT SHOULD BE ONE FOR ALL OF THEM
rightButton.tag = annotation.hash
annotationView.canShowCallout = true
annotationView.rightCalloutAccessoryView = rightButton
rightButton.addTarget(self, action: #selector(didClickDetailDisclosure(button:)), for: .touchUpInside)
let leftButton = UIButton(frame: CGRect(
origin: CGPoint.zero,
size: CGSize(width: 25, height: 25)))
leftButton.setBackgroundImage(#imageLiteral(resourceName: "nav"), for: .normal)
annotationView.leftCalloutAccessoryView = leftButton
leftButton.addTarget(self, action: #selector(didClickDetailDisclosure(button:)), for: .touchUpInside)
switch annotation.title!! {
case "One":
annotationView.markerTintColor = UIColor.gray
annotationView.glyphImage = UIImage(named: "kopula")
case "Two":
annotationView.markerTintColor = UIColor.black
annotationView.glyphImage = UIImage(named: "two")
rightButton.addTarget(self, action: #selector(didClickDetailDisclosure2(button:)), for: .touchUpInside)
case "Three":
annotationView.markerTintColor = UIColor.red
annotationView.glyphImage = UIImage(named: "three")
rightButton.addTarget(self, action: #selector(didClickDetailDisclosure3(button:)), for: .touchUpInside)
default:
annotationView.markerTintColor = UIColor.darkGray
annotationView.glyphImage = UIImage(named: "place")
rightButton.addTarget(self, action: #selector(didClickDetailDisclosure(button:)), for: .touchUpInside)
}
return annotationView
}
}
I have added Scroll View and Stack View in it, but it does not seems to be the solution. I would like to have an effect like below: One photo and 3 labels under the photo in a container. I have 12 items like this, that is why I need ScrollView. Any ideas how to make it step by step? Here is the solution (airbnb app) I would like to have (I needed to put it on imgur, because there was an error, while adding a photo here):
https://imgur.com/pFY3DX5