How to centered on the x-axis?

f78e8fb5afe04291a4aa1ba61c5ca662.png
dateNow.frame = CGRect(x: screen.width/2 , y: 50, width: screen.width/3 , height: screen.height/4)


Why not at the center. And as centered on the X-axis ???
July 2nd 19 at 16:56
3 answers
July 2nd 19 at 16:58
Solution
The left side of the square you have is clearly at the center of the screen, so that the current code runs correctly. Below the square was the center on the X-axis need to move it to the left by half its width. That is, the code should be:
dateNow.frame = CGRect(x: screen.width/2 - screen.width/3/2 , y: 50, width: screen.width/3 , height: screen.height/4)
July 2nd 19 at 17:00
Apple recommends to use Autolayout, and not frames, and there is a reference to the Central axis: NSLayoutAttributeCenterX, NSLayoutAttributeCenterY, you can add and offset to them. Moreover, You can do this in IB (storyboard or xib) and your code with VFL (Visual Format Language).
July 2nd 19 at 17:02
another possible constraint to configure

Find more questions by tags SwiftiOS