How to get the dimensions of a view in iOS App?

iOSApps/ApplicationsMobile Development

Getting dimensions of a view is easy, In this post, we will see how one can get the dimensions of a view in iOS.

Let’s get started,

Step 1 − Open Xcode and create a single view application and name it SampleView.

Step 2 − Open Main.storyboard and add UIView as shown below,

Step 3 − Create @IBOutlet of the view in ViewController.swift.

@IBOutlet var sampleView: UIView!

Step 4 − In viewDidLoad of ViewController.swift write the following code, print(sampleView.frame.size)

Run the application to see the size.

Complete code

import UIKit
class ViewController: UIViewController {
   @IBOutlet var sampleView: UIView!
   override func viewDidLoad() {
      super.viewDidLoad()
      print(sampleView.frame.size)
   }
}
raja
Published on 30-Aug-2019 14:35:52
Advertisements