Savca Marin
Savca Marin

Reputation: 437

Frame origin is different from the one set

In the viewWillLayoutSubviews I set view's frame y origin to UIScreen.main.bounds.height, which means that the view should not be visible since it's below everything (I do call the super.viewWillLayoutSubviews on top of everything).

In the view hierarchy inspector the view looks exactly as it is supposed to be, but on real device you can see lower how it looks, it can be seen on the bottom of the screen on a real device, even if the origin is the screen height.

view hierarchy

real device view

My question is why is the custom view looking different on the real device. Also what is causing this strange bug? Any feedback is greatly appreciated, thanks in advance.

Edit: This visual bug happens on all the devices except for the iPhone X

Edit2: This is the code I'm using to set the view's frame

customView.frame.origin.x = 0
customView.frame.origin.y = UIScreen.main.bounds.height     
customView.frame.size.width = UIScreen.main.bounds.width
customView.frame.size.height = UIScreen.main.bounds.height * 0.72

Edit3: Found the bug, my bad I didn't post this code from the beginning, I never thought the bug could happen because of this.

    let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: customView.frame.width, height: customView.frame.height))
    imgView.contentMode = .scaleAspectFill
    imgView.image = gradientImage
    customView.addSubview(imgView)
    customView.sendSubview(toBack: imgView)

The bug comes from this line:

imgView.contentMode = .scaleAspectFill

Changed this to:

imgView.contentMode = .scaleToFill

Very very sorry for not mentioning this from the beginning, my fault, I'd like to thank everyone for their help, also thanks to @matt for pointing out that this code should be in the viewDidLayoutSubviews().

Upvotes: 1

Views: 169

Answers (1)

TylerP
TylerP

Reputation: 9829

Looks like your custom view either is (or contains) a UIImageView, possibly with a contentMode set to .aspectFill. If that's the case, try setting that image view's clipsToBounds to true (or check the equivalent checkbox in Interface Builder).

Image views with contentModes set to .aspectFill can actually have their content spill out of the image view's bounds, so that looks to be what's happening here.

Upvotes: 1

Related Questions