Get the view displayed on the screen in iOS development

Source: Internet
Author: User

In iOS development, we often encounter a problem, for example, click on a button, pop up a mask layer, above the display of a box, the location of the box appears near the button. If the position of the button is fixed relative to the edge of the screen, it is easy to write the dead position directly. But what if the button is on the UITableView cell? As the UITableView scrolls, the buttons may be at the top or at the bottom, possibly in the middle, on the left and right, so how do you calculate the position of the button at this time? What if the Uitabelview of the button is on a cell in another uiscrollview? What if there's another rolling layer out there? This layout is really complicated.

There is a requirement in the company's project recently, the level used is a uitableview nested a uitableview and a uicollectionview, and there is a click button, the effect is similar to the headline information list of the small fork, Click on the button next to pop up a view, to screen information and other operations. I pop a top mask layer when I click the button and add an operation area to the mask, but the position of the operation area needs to be determined according to the position of the button, so it took a little time to write a method to find the position of the button on the screen, relative to the screen;

In fact, the code is not much, just need to write a UIView extension method on the line

Extension UIView {func zhmfpositioninscreen ()-cgpoint {//////First to determine if there is a parent view, if there is no parent view, return to the position of the view directly if let S Uperview = Self.superview {/** Determines whether the parent view is Uiscrollview or inherits from Uiscrollview first use view in                         The location on the screen uses the position of the view with the position of the parent view x and Y separately if the parent view is not Uiscrollview and does not inherit from Uiscrollview, the result is returned directly            If the parent view is Uiscrollview or inherited from Uiscrollview, you also need to subtract Uiscrollview scrollviewoffset.x and Scrollviewoffset.y separately, and then return the result */If let ScrollView = Superview as? Uiscrollview {Let position = Cgpoint.init (x:self.frame.origin.x, Y:SELF.FRAME.ORIGIN.Y) L                ET superposition = superview.zhmfpositioninscreen () Let Scrollviewoffset = Scrollview.contentoffset Return Cgpoint.init (x:superposition.x + position.x-scrollviewoffset.x, Y:SUPERPOSITION.Y + Position.y-scrol LVIEWOFFSET.Y)} else {Let superposition = superview.zhmfpositioninscReen () Let position = Self.frame.origin return Cgpoint.init (x:superposition.x + position.x, Y:SUPERPOSITION.Y + position.y)}} else {return Self.frame.origin}}}

The extension method to get the view's position on the screen has been written, just need to be used in the use of a tune just fine.

button.zhmfPositionInScreen()

Although the code is small, but also spent a little time to think logically, did not go to Baidu and other search, can write their own, or spend their time to think about it, otherwise, the brain lazy, it really can only be yards of farmers.

iOS development, focus on Swift, if you want to communicate, always welcome, look forward to progress together!!!

Get the view displayed on the screen in iOS development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.