1

enter image description here

Can anyone explain why the orange square is not at the 0 point, and i can still scroll my scroll view up and down by the 20 pixels?

import UIKit

class MessageVC: UIViewController, UIScrollViewDelegate {

let scrollView = UIScrollView()


let messageHolder = MessageHolderVC()
let messageSender = MessageSenderVC()

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.whiteColor()


    scrollView.frame = CGRectMake(0, 1, self.view.frame.width, self.view.frame.height - 2)
    scrollView.backgroundColor = UIColor.redColor()
    scrollView.pagingEnabled = true

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 2, scrollView.frame.size.height)
    scrollView.bounces = false

    self.view.addSubview(scrollView)


    messageHolder.view.frame = CGRectMake(0, 0, 20, 20)
    scrollView.addSubview(messageHolder.view)




}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/
stktrc
  • 1,599
  • 18
  • 30

1 Answers1

0

Try this: in your storyboard select the ViewController and uncheck Adjust Scroll View insets, like this:

Screenshot

Islam
  • 3,654
  • 3
  • 30
  • 40
  • Ah sorry entirely done in code. I can do that in code though. It never used to be an issue, so i'm guessing it's with something new. – stktrc Nov 30 '15 at 05:18
  • Then try with this: `self.automaticallyAdjustsScrollViewInsets = false` – Islam Nov 30 '15 at 05:21
  • And that worked. Thank you Mr Q. Would you mine pointing me in the direction of a little more understanding on that please? – stktrc Nov 30 '15 at 05:27
  • [here's](http://stackoverflow.com/questions/1983463/whats-the-uiscrollview-contentinset-property-for) some Q&A about scroll view content inset. – seto nugroho Nov 30 '15 at 05:37
  • With iOS 7 Apple introduced the translucent nav bars so for that the actual frame of the `scrollView` would be placed at the top and the content would start from `statusBarHeight + navBarHeight` position so when scrolling up your content goes behind the nav bar to create that effect. In your case you don't have a nav bar and that's why only getting the `statusBarHeight` added – Islam Nov 30 '15 at 05:37