senty
senty

Reputation: 12857

Additional Offset X on Horizontally Centered Element

I have a SubView inside another View that has other elements inside it. I horizontally centered the middle element of SubView to SubView, but I need to give it an extra horizontal offset. Is there a way to be able to use both Horizontal Center and give extra offset?

Unfortunately, this isn't work:

override func viewDidLoad() {
   super.viewDidLoad()

        MyView.frame.origin.x += 50
   }

Upvotes: 0

Views: 32

Answers (2)

André Slotta
André Slotta

Reputation: 14040

you were already heading into the right direction...

override func viewDidLoad() {
    super.viewDidLoad()

    let frame = MyView.frame
    frame.origin.x += 50
    MyView.frame = frame
}

if you are using autolayout though you have to modify constraints instead of modifying frames directly.

Upvotes: 0

Nishant
Nishant

Reputation: 12617

Just change the constant value from 0 to 50 (or any value of choice) to give an offset to horizontal alignment.

See picture below:

enter image description here

Upvotes: 1

Related Questions