iOS Swift Setting View Frame Position Programmatically

17,756

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).

Share:
17,756
Kavin Kumar Arumugam
Author by

Kavin Kumar Arumugam

3 years of experience in iOS application development with 2.3 years of industrial experience. Skilled IT professional and specialised in the entire iOS development cycle. Interested to work with more challenging position with an organisation where innovation and excellence is the way of life.

Updated on June 26, 2022

Comments

  • Kavin Kumar Arumugam
    Kavin Kumar Arumugam almost 2 years

    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

  • Kavin Kumar Arumugam
    Kavin Kumar Arumugam about 7 years
    It work but when I scrolling my scrollview both view are back to old position
  • Paulo Mattos
    Paulo Mattos about 7 years
    @KavinKumarArumugam Where do you run the frame setting code in your question?
  • Kavin Kumar Arumugam
    Kavin Kumar Arumugam about 7 years
    Inside viewDidAppear
  • Paulo Mattos
    Paulo Mattos about 7 years
    @KavinKumarArumugam If it is inside that function (and not inside a closure) you can safely drop the DispatchQueue.main.async wrapper then — you already on the main thread :)