0

It appears that setting the text of a label can have the side effect of increasing the opacity of a MKCircle that has been added to an MKMapView.

Here is what's going on:

First I have this delegate function to handle rendering my overylay

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let diskRenderer: MKCircleRenderer = MKCircleRenderer.init(overlay: overlay)
        diskRenderer.fillColor = UIColor.init(red: 0, green: 255, blue: 255, alpha: 0.10)
        diskRenderer.strokeColor = UIColor.init(red:0, green: 0, blue: 0, alpha: 0.25)
        return diskRenderer
    }

Then I add it to the MKMapView

map.delegate = self
let diskOverlay: MKCircle = MKCircle.init(center: location, radius: diskRadius)
map.add(diskOverlay)

All seems good. Now at the same time, I have a custom UIControl called YSRangeSlider that I got from cocoapods. When the user moves the thumbs on this slider, a function gets called that updates the text of some labels according to whatever the current values are on the slider. Here is that function.

func setPlayerLabels() {
        let lowerValue = Int(playersSlider.minimumSelectedValue)
        let upperValue = Int(playersSlider.maximumSelectedValue)
        minPlayersLabel.text = "\(lowerValue)"
        maxPlayersLabel.text = "\(upperValue)"
    }

Now here is the really really weird thing. When I launch the app and segue to this ViewController and then start sliding a thumb on the slider, the MKCircle - that is the faint disk being shown on the map, becomes more and more opaque until it is solid filled. But if I comment out the two label.text = "---" going on in the function above, this strange behavior no longer occurs.

Below you can see the overlay as it looks initially (low opacity) and then after getting messed up (opaque). I don't know the deeper reason of why this is happening nor how to fix it. This is XCode 8 with Swift 3.

  • To make things even worse, I should mention that sometimes the map renders the disk overlay as if it was on a large square tile casting a shadow beneath it, a shadow who's opacity goes towards opaque just like the overlay itself. Also if I zoom out some before touching the slider, it so happens that sliding a thumb on the slider then causes the map to zoom back in on top of the already mentioned opacity changes.

enter image description hereenter image description here

user3915477
  • 275
  • 4
  • 11

1 Answers1

0

For anyone interested, I figured out how to solve this problem, however the fundamental cause and effect behind it is still a bit lost on me.

What's happening is whenever those label.text = "---" assignments get called, the viewDidLayoutSubviews() function also gets called. Since the MKMapView's overlay and region get set inside viewDidLayoutSubviews, it so happens that the overlay gets redrawn on top of the existing one, making it appear to get darker and darker, and hence the map zooms back into the region if I had already zoomed out.

What I still don't understand is why executing label.text = "---" causes the viewDidLayoutSubviews function to get called.

user3915477
  • 275
  • 4
  • 11
  • Searching around StackOverflow you can find some answers as to why setting the text of a UILabel causes the viewDidLayoutSubviews to fire. In my case, a solution here is to wrap all the code inside of viewDidLayoutSubviews in a boolean if statement so that it will only ever fire once. – user3915477 Sep 25 '16 at 16:58