F
F
FlooDwm2016-11-14 22:00:12
iOS
FlooDwm, 2016-11-14 22:00:12

How to center on the x-axis?

f78e8fb5afe04291a4aa1ba61c5ca662.png

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

Why is it not in the center? And how to center on the x-axis ???

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
vaux, 2016-11-14
@FlooDwm

The left side of the square is clearly centered on the screen, so that the current code runs correctly. In order for the square itself to be centered along the X axis, you need to additionally move it to the left by half its width. That is, the code should be like this:

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

I
Igor Cherny, 2016-11-15
@freeg0r

Apple recommends using Autolayout, not frames, and there are bindings to the central axes: NSLayoutAttributeCenterX, NSLayoutAttributeCenterY, you can add offsets for them. Moreover, you can do this both in IB (storyboard or xib), and in code using VFL (Visual Format Language).

D
Denis Fedorets, 2016-11-15
@fedorez

you can also set constraint

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question