Search code examples
iosswiftipadmkmapviewcgrectmake

Swift: Finding size of element programatically on different devices


For my job I am helping to design an iOS app and so I recently started working with Swift. In the app we have three different elements: a Google maps element and two graphs. I want to make it so that if you press on any of these elements they will resize to fit the screen and when you press the screen again they will go back to their original size.

Filling the screen is no problem and I already have successfully gotten that implemented. However, when you press again the element appears smaller or larger than it originally was, depending on what device you run it on. Currently I'm minimizing it by using the height and width attributes of the element and just setting them back to the original values for these attributes when the user presses the full screen element. However, it seems that these are hard coded in rather than being dynamically created for whatever device you are on and this is the reason why it won't correctly go back to it's original size.

Here is currently what I have in my code:

@IBAction func mapButtonClicked(sender: AnyObject) {
    if mapExpanded == false {
        mapView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
        mapButton.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
        mapExpanded = true
    } else {
        mapView.frame = CGRectMake(0, 0, oldMapWidth, oldMapHeight)
        mapButton.frame = CGRectMake(0, 0, oldMapWidth, oldMapHeight)
        mapExpanded = false
    }
}

where oldMapWidth = self.view.mapView.width and oldMapHeight = self.view.mapView.height.

I have tried to resize it relative to the screen but that only works on a single device and gives the same issues when switching to a different tablet. Is there a different attribute that will provide the height and width after compilation so that it will be correct for the current device it is on?


Solution

  • What about preserving the frame, and setting it before expanding? This test code seems to work perfectly for me:

    class ViewController: UIViewController {
    
        @IBOutlet weak var mapView: MKMapView!
        @IBOutlet weak var mapButton: UIButton!
        var mapExpanded = false
        var oldFrame = CGRectZero
    
        @IBAction func mapButtonClicked(sender: UIButton) {
            if mapExpanded == false {
                // before expanding, set old
                oldFrame = mapView.frame
    
                mapView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
                mapButton.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
                mapExpanded = true
            } else {
                mapView.frame = oldFrame
                mapButton.frame = oldFrame
                mapExpanded = false
            }
        }
    }
    

    In my example I am using AutoLayout in the Storyboard to place the map and button. I recommend doing the same.