Search code examples
iosxcodeswiftcgrect

How to programatically adjust x and y axis of image in Swift?


I am trying to centre my round image in the middle of my screen. (I have a video playing underneath it). I cannot seem to get this to centre based on the screen size. I am sure it is a simple solution , but if anyone has any ideas I would be very grateful

Here is the image of my simulator:

And here is my code:

    imageViewObject = UIImageView (frame: CGRectMake(CGRectGetMidX(self.view.frame), 150, 150, 150))
    imageViewObject.image = UIImage(named:"one.png")
    self.view.addSubview(imageViewObject)

Thank you in advance for any help!


Solution

  • The problem is that the position origin is the top left corner. You should use something like this instead:

    imageViewObject = UIImageView (frame: CGRectMake(CGRectGetMidX(self.view.frame) - 150/2, 150, 150, 150))