Khan Luke
Khan Luke

Reputation: 168

How to re-position UILabel in the exact same position as another UILabel

I'm trying to re-position a UILabel exactly over another UILabel (identical position). Its not positioning itself exactly over the target UILabel.

See code below

let element1yPosition = element1.frame.origin.y. // Element 1 y Position

let element2yPosition = element2.frame.origin.y. // Element 2 y Position

UIView.animate(withDuration: 1.0, animations: {

    element1.frame.origin.y = element2yPosition     // Repositioning stage. 
                                                   //Fails because Element 1 doesn't re-position itself in the exact same position as Element 2 as required.

})

Upvotes: 0

Views: 74

Answers (2)

ChokWah
ChokWah

Reputation: 105

element1.frame.origin.y is a number in struct. element1 didn't know his's frame.origin.y changed. if You want to change a UIView's position, you should change it's frame,center,transform.

In Objective-C, it's wrong.enter image description here

That's the right way: enter image description here

Upvotes: -1

Shehata Gamal
Shehata Gamal

Reputation: 100503

Can you try

UIView.animate(withDuration: 1.0, animations: {

   element1.frame = element2.frame 

})

Upvotes: 2

Related Questions