SoFunction
Updated on 2025-04-12

iOS development: How to get the location where the view is displayed on the screen

Preface

I believe that everyone often encounters a problem in iOS development. For example, clicking a button will pop up a mask layer, and a pop-up frame will be displayed, and the pop-up frame will be displayed near the button. If the distance between the position of this button and the edge of the screen is fixed, it will be easy and you can directly write the position of the button. But what if the button is on the UITableView cell? As the UITableView scrolls, the button may be at the top, the bottom, or the middle, both on the left and right. So, how to calculate the position of the button at this time? What if the UITabelView where the button is located is on a cell of another UIScrollView? What if there is another rolling layer outside? This layout is indeed very complicated.

Recently, there is a requirement in the company's project. The level used is that a UITableView and a UICollectionView are nested in a UITableView, and there is also a click button. The effect is similar to the small cross of the headline information list. Click to pop up a view next to the button to block the information and other operations. When I clicked the button, a top-level mask layer popped up, adding an operation area to the mask. However, the position of the operation area needs to be determined based on the position of the button, so I spent some time writing a method to find the position of the button on the screen, relative to the screen;

The method is as follows:

In fact, there is not much code, just write a UIView extension method.

extension UIView {
 func zhmfPositionInScreen() -> CGPoint {
 /// First determine whether there is a parent view. If there is no parent view, just return to the view position directly if let superView =  {
  /**
   Determine whether the parent view is a UIScrollView or inherited from a UIScrollView
  
   First use the position of the view on the screen to add the position of the view and the position of the parent view X and Y respectively.
  
   If the parent view is not a UIScrollView and does not inherit from a UIScrollView, the result will be returned directly
  
   If the parent view is a UIScrollView or inherits from a UIScrollView

   You also need to subtract the sum of the UIScrollView separately and return the result
   */
  if let scrollView = superView as? UIScrollView {
  let position = (x: , y: )
  let superPosition = ()
  let scrollViewOffset = 
  return (x:  +  -  , y:  +  - )
  } else {
  let superPosition = ()
  let position = 
  return (x:  + , y:  + )
  }
 } else {
  return 
 }
 }
}

The method to obtain the position of the view on the screen has been written, just adjust it when using it.

()

Although the code volume is not large, I spent some time thinking about logic at that time. I didn’t go to Baidu for searching. If I could write it myself, I would have to spend time thinking about it myself. Otherwise, if I am lazy, I will be really a coder.

ios Get the position of the control relative to the screen

The object to be obtained is view1, then the position of the view relative to the screen can be implemented using the following method:

UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
 CGRect rect=[view1 convertRect:  toView:window];

Summarize

The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.