Monday, September 21, 2026
HomeiOS Developmentios - Updating Label of Customized View Programmatically

ios – Updating Label of Customized View Programmatically

[ad_1]

I’m engaged on an app which requires the UIView properties to alter each second. It’s a map utility and each time I get a brand new location I wish to replace the textual content of my customized view.

All my customized views are saved in an array as proven under:

non-public func setupAnnotationViews() {
        
        for coordinate in path.coordinates {
            
            let coordinate = CLLocationCoordinate2D(latitude: coordinate.0, longitude: coordinate.1)
            let location = CLLocation(coordinate: coordinate, altitude: altitude)
            let annotationView = AnnotationView(body: CGRect(x: 0, y: 0, width: 150, top: 60))
            annotations.append(annotationView)
           
        }
    }

Now, once I get a brand new location replace I hearth the plot perform:

 func locationManager(_ supervisor: CLLocationManager, didUpdateLocations areas: [CLLocation]) {
        DispatchQueue.fundamental.async {
            self.plot()
        }
    }

Contained in the plot perform I wish to replace the label and distanceLabel properties.

for (index, coordinate) in path.coordinates.enumerated() {
            
            let coordinate = CLLocationCoordinate2D(latitude: coordinate.0, longitude: coordinate.1)
            let location = CLLocation(coordinate: coordinate, altitude: altitude)
            
            let annotationView = annotations[index]
            annotationView.label.textual content = "(index) + 1"
            let distanceInMeters = locationManager.location?.distance(from: location)
            let miles = distanceInMeters! * 0.000621371192
            annotationView.distanceLabel.textual content = String(format: "%.2f miles", miles)
            annotationView.label.setNeedsDisplay()
        }

My AnnotationView is carried out under:

class AnnotationView: UIView {
    
    lazy var label: UILabel = {
        let label = UILabel(body: CGRect(x: 0, y: 0, width: 100, top: 20))
        label.layer.masksToBounds = true
        label.layer.cornerRadius = 10
        label.textAlignment = .middle
        label.textColor = .white
        label.font = UIFont.boldSystemFont(ofSize: 18.0)
        return label
    }()
    
    lazy var distanceLabel: UILabel = {
        let distanceLabel = UILabel(body: CGRect(x: 0, y: 0, width: 100, top: 20))
        distanceLabel.textColor = .white
        distanceLabel.textAlignment = .middle
        return distanceLabel
    }()
    
    override init(body: CGRect) {
        tremendous.init(body: body)
        setup()
    }
    
    non-public func setup() {
      
        let stackView = UIStackView(body: CGRect(x: 0, y: 0, width: 150, top: 60))
        stackView.addArrangedSubview(label)
        stackView.addArrangedSubview(distanceLabel)
        stackView.axis = .vertical
        stackView.backgroundColor = .black
        stackView.layer.masksToBounds = true
        stackView.layer.cornerRadius = 10
        self.addSubview(stackView)
    }
    
}

Although plot perform is getting fired, the AnnotationView label or distanceLabel just isn’t getting up to date.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments