Solved: get distance between two uiview

Creating distance between two UIViews is often needed in mobile app development. It allows flexibility, balance and aesthetic appeal in app layout. The concept of UIViews in Swift gives iOS developers the power to modify the user interface of an app programmatically. By applying Swift’s core functions and libraries, we can successfully compute the distance between two UIViews.

Understanding how UIView works is key to decipher this dilemma. UIView is an integral part of UIKit framework and serves as the basic view class for constructing and managing a view hierarchy. Therefore, to understand distances between UIViews and to manage spaces in iOS interface, one must involve UIKit library and Swift’s core functions.

UIKit Framework and UIView

Primarily, UIKit framework forms the core framework for creating graphical, event-driven applications in iOS. It provides windows and UIViews which together form the user interface and react to both user and system events. Therefore, laying out UI requires careful consideration of UIViews’ positioning.

UIView is an integral part of UIKit. It’s a class that defines a rectangular area on the screen and the interfaces for managing content in this area. All UI elements that are drawn on screen are UIView or subclass of UIView. It could be a button, a label, a text field or a view itself.

Computing Distance Between Two UIViews

To compute distance, we tap into the power of Swift’s geometry operators. The base library in Swift includes geometry functions that work together with UIViews.

func getDistanceBetween(viewA: UIView, viewB: UIView) -> CGFloat {
    let centerA =
    let centerB =
    let distance = sqrt(pow(centerA.x - centerB.x, 2) + pow(centerA.y - centerB.y, 2))
    return distance

This function accepts two UIViews as parameters, identifies their center points, and ultimately computes the distance using the Pythagorean Theorem.

Understanding the Code

The function defined above deploys the Pythagorean Theorems in calculating the distance between two UIViews. We get the center points of the two UIViews respectively, by acquiring their points on the x and y axes. The points are derived from the .center property of the UIView.

let centerA =
let centerB =

The calculated distance is the length of the hypotenuse, the straight line between the two UIViews. This is computed by the sqrt() function.

let distance = sqrt(pow(centerA.x - centerB.x, 2) + pow(centerA.y - centerB.y, 2))

The pow function in Swift calculates the value of a base raised to an exponent. In this case, it gives us the square of the distance in both X and Y directions. The sum of these distances is then square rooted to give the final distance.

Managing UIViews in your Swift code makes it possible to greatly affect the look and feel of an app’s interface. If you understand the relationship of UIViews and handle the UIViews properly, you can make visually balanced, clean and intuitive user interfaces. This aspect of programming styling can be compared to the trends and combinations of different fashion styles. Just like how distinct cuts, patterns, and colors come together harmoniously to form an entire outfit, each UIView is positioned and styled according to your app’s overall theme and design.

Related posts:

Leave a Comment