Search code examples
iosswiftuiscrollviewuikitcgrect

iOS Swift Setting View Frame Position Programmatically


I having two sub views inside scrollview. I need to position that both subviews programmatically. I did it correctly by writing code inside DispatchQueue.main.async. Here is the code:

DispatchQueue.main.async {
    self.SelectClientDetailView.frame = CGRect(x: 0, y: 637, width: self.SelectClientDetailView.frame.size.width, height: self.SelectClientDetailView.frame.size.height)
    self.SelectClientDetailView2.frame = CGRect(x: 0, y: 837, width: self.SelectClientDetailView2.frame.size.width, height: self.SelectClientDetailView2.frame.size.height)
}

Its working good but when I scrolling my scrollview this both views set back to old positions. How to fix it. Its Default y position will be SelectClientDetailView:400 and SelectClientDetailView2: 600


Solution

  • If you only want to change the frame Y position, try this instead:

    self.SelectClientDetailView.frame.origin.y = 637
    self.SelectClientDetailView.frame.origin.y = 837
    

    As already mentioned, you might need to check your view hierarchy to be sure you are actually adding them to the UIScrollView (and not elsewhere).