Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check to see if one view is on top of another view?

Currently there is a scrollview (whole screen) that sits behind a uiview (bottom portion of the screen). I"m trying to check whether or not a label on the scroll is covered by the uiview (this happens with iphone se). Is there a way to check if the uiview is covering the label on the scrollview? I tried using the center cgpoint of a label on the scrollview to see if its smaller than a point on the uiview but it won't allow me to compare two cgpoints for size:

if atmLabel.center < CGPoint(x: 156, y: 570) {
    buttonView.dropShadow(shadowOpacity: 0.5)
}
like image 535
SwiftyJD Avatar asked Sep 26 '17 22:09

SwiftyJD


Video Answer


2 Answers

For point checking, you should use

contains function of CGRect... in short this should work

 let view = UIView()
 let label = UILabel()

 if label.frame.contains(view.frame) {
    //Do your stuff...
 }

You can use the view hiearchy with Xcode with this button enter image description here

Or you can simply print out on the current viewController subviews

print(self.view.subviews)

this prints out array of UIView subclasses sorted by ascending order of layer on the view... I recommend the first way of debugging :)

like image 64
Dominik Bucher Avatar answered Oct 21 '22 20:10

Dominik Bucher


If you want to see if two views overlap in code, I think this should work:

if view1.frame.intersects(view2.frame) {
    // handle overlap
}
like image 24
Charles Srstka Avatar answered Oct 21 '22 22:10

Charles Srstka