1

I am have scrollview in my app. By default y position of my scrollview is 80. In some locations I need to move y position of my scrollview to 0. Here is the code

@IBOutlet weak var ScrollView: UIScrollView!
override func viewDidLoad()
{
   super.viewDidLoad()
   self.ScrollView.delegate = self
   if(Position == "changed")
   {
      self.ScrollView.frame = CGRect(x: 0, y: 0, width: self.ScrollView.frame.size.width, height: self.ScrollView.frame.size.height)
   }
   else
   {
      self.ScrollView.frame = CGRect(x: 0, y: 80, width: self.ScrollView.frame.size.width, height: self.ScrollView.frame.size.height)
   }
}

But this code won't works.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kavin Kumar Arumugam
  • 1,792
  • 3
  • 28
  • 47

2 Answers2

1

problem is simple your VC.view not updated in Viewdidload, so in here you need to move the UI updation on Viewdidappear or update the UI in main thread

by default your self.ScrollView.frame is started in O position, so only check with !=

override func viewDidLoad()
{
   super.viewDidLoad()
   self.ScrollView.delegate = self
   //self.ScrollView.frame = CGRect(x: 0, y: 0, width: self.ScrollView.frame.size.width, height: self.ScrollView.frame.size.height)
   if(Position != "changed")
   {
     DispatchQueue.main.async {
        self.ScrollView.frame = CGRect(x: 0, y: 80, width: self.ScrollView.frame.size.width, height: self.ScrollView.frame.size.height)
    }

   }

}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

We will not able to set Scrollview Position in viewDidLoad() so Add those code in viewDidAppear(). Final Code Here

override func viewDidAppear(_ animated: Bool)
{
     DispatchQueue.main.async
     {
          self.ScrollView.frame = CGRect(x: 0, y: 0, width: self.ScrollView.frame.size.width, height: self.ScrollView.frame.size.height + 80)
     }
}
Kavin Kumar Arumugam
  • 1,792
  • 3
  • 28
  • 47
  • if your VC embed in navigation controller , the UI will render correctly for your concept if you navigate once to another VC and come back – Anbu.Karthik Oct 11 '17 at 07:02