8 Replies
      Latest reply on Feb 14, 2020 3:58 PM by UNIVAC Coder
      UNIVAC Coder Level 1 Level 1 (0 points)

        I need sample Xcode Swift code for a macOS app that shows the code for a Scroll View.

        • Re: How to code a Scroll View
          Claude31 Level 8 Level 8 (8,085 points)

          What is it you do not know how to code ?

           

          A key point for scrollView is to define a content size large enough (more than the scrollView).

           

           

          In fact,scrollView is a "window" you move over the content, giving the illusion that the view is moving.

            • Re: How to code a Scroll View
              UNIVAC Coder Level 1 Level 1 (0 points)

                   Here is my best effor to learn about NSScrollView:

              In a macOS App: change viewDidLoad() to setup, add a scrollView to the storyboard, and run to show a Scrolling view?:

               

                      override func viewDidLoad() {

                      super.viewDidLoad()

              //Create a Big documentView and fill with array of NSButtons?

                      let myDocView = NSView(frame: NSRect(x: 0, y: 0, width: 2000, height: 2000))

                      let ymax = Int(myDocView.frame.size.height)

                      for row in 1...9 {

                            for col in 1...9 {

                                let aButton = NSButton(frame: NSRect(x: col * 44 + 10, y: ymax - (row * 44 + 10), width: 32, height: 37))

                                aButton.title = row.description + "/" + col.description

                                myDocView.addSubview(aButton)

                            }

                        }

              //then find the NSScrollView added into the View in the storyboard with?:

                      let arrayOfViews = self.view.subviews

                     print("arrayviews is \(arrayOfViews)")

                      let mySV = arrayOfViews[0] as! NSScrollView

                      print("ScrollView is \(mySV)")

                      mySV.scrollerStyle = .overlay

                      mySV.hasVerticalScroller = true

                      mySV.hasHorizontalScroller = true

                      mySV.autohidesScrollers = false

              //then add my documentView?

                      mySV.documentView = myDocView

               

                  }

               

              I am at a loss for understanding and any help here is appreciated

                • Re: How to code a Scroll View
                  Claude31 Level 8 Level 8 (8,085 points)

                  Why don't you create the scrollView in IB directly ?

                    • Re: How to code a Scroll View
                      UNIVAC Coder Level 1 Level 1 (0 points)

                      I did use IB?(storyboard) to create a NSScrollView and use the code below to find the view as I usually assign a tag number to views but with NSScrollview I am unable to assign a tag number:

                              let arrayOfViews = self.view.subviews

                             print("arrayviews is \(arrayOfViews)")

                              let mySV = arrayOfViews[0] as! NSScrollView

                              print("ScrollView is \(mySV)")

                       

                      Do I create a Scroll view using only IB? Does it have anything to do with the constraints; I removed most of the constraints and observed no scroll bars.

                        • Re: How to code a Scroll View
                          UNIVAC Coder Level 1 Level 1 (0 points)

                          This code below worked once I changed my storyboard; I removed most of the constraints on the views  and resized the Scrollview to be much smaller then the main view. ran the app and it started working.  I dont know how or understand why.

                           

                          class ViewController: NSViewController {

                           

                                 override func viewDidLoad() {

                                  super.viewDidLoad()

                                  let myDocView = NSView(frame: NSRect(x: 0, y: 0, width: 2000, height: 2000))

                                  let ymax = Int(myDocView.frame.size.height)

                                  for row in 1...9 {

                                        for col in 1...9 {

                                            let aButton = NSButton(frame: NSRect(x: col * 44 + 10, y: ymax - (row * 44 + 10), width: 32, height: 37))

                                            aButton.title = row.description + "/" + col.description

                                            myDocView.addSubview(aButton)

                                        }

                                    }

                               

                                  self.view.addSubview(myDocView)

                                  let aButton = NSButton(frame: NSRect(x: 20, y: 20, width: 32, height: 37))

                                  aButton.title = "aa"

                                  self.view.addSubview(aButton)

                                

                                  let arrayOfViews = self.view.subviews

                                 print("arrayviews is \(arrayOfViews)")

                                  let mySV = arrayOfViews[0] as! NSScrollView

                                  print("ScrollView is \(mySV)")

                                  mySV.scrollerStyle = .overlay

                                  mySV.hasVerticalScroller = true

                                  mySV.hasHorizontalScroller = true

                                  mySV.autohidesScrollers = false

                                  mySV.documentView = myDocView

                                

                                  aButton.title = "BB"

                                  mySV.addSubview(aButton)

                            

                              }

                            • Re: How to code a Scroll View
                              UNIVAC Coder Level 1 Level 1 (0 points)

                              Here is a simplified code for viewDidLoad():

                                    override func viewDidLoad() {

                                      super.viewDidLoad()

                                      let myDocView = NSView(frame: NSRect(x: 0, y: 0, width: 2000, height: 2000))

                                      let ymax = Int(myDocView.frame.size.height)

                                      for row in 1...9 {

                                            for col in 1...9 {

                                                let aButton = NSButton(frame: NSRect(x: col * 44 + 10, y: ymax - (row * 44 + 10), width: 32, height: 37))

                                                aButton.title = row.description + "/" + col.description

                                                myDocView.addSubview(aButton)

                                            }

                                        }

                                    

                                      let arrayOfViews = self.view.subviews

                                      print("arrayviews is \(arrayOfViews)")

                                      let mySV = arrayOfViews[0] as! NSScrollView

                                      print("ScrollView is \(mySV)")

                                      mySV.scrollerStyle = .overlay

                                      mySV.hasVerticalScroller = true

                                      mySV.hasHorizontalScroller = true

                                      mySV.autohidesScrollers = false

                                      mySV.documentView = myDocView

                                

                                  }

                              Drag an NSScrollView from the library into the view in the storyboard and play with the constraints and view size.

                              I played with the constraints and size of the NSScrollView and found that I can add back all constraints and expand the Scrolll view to fill the main view.  It will work; I don't understand how.

                                • Re: How to code a Scroll View
                                  Claude31 Level 8 Level 8 (8,085 points)

                                  Sure, the way you define constraints for a scrollView is very critical.

                                   

                                  Inadequate constraints "freezes" the view.

                                   

                                  May have a look at this (long) thread (iOS, but similar to MacOS)

                                  https://forums.developer.apple.com/message/402000#402000

                                   

                                  Or read this tutorial

                                  h ttps://useyourloaf.com/blog/scroll-view-layouts-with-interface-builder/

                                   

                                    • Re: How to code a Scroll View
                                      UNIVAC Coder Level 1 Level 1 (0 points)

                                      Thank you for your considerations.  I learned that three things are neseccary for a NSScrollView which are not documented clearly and there is absolutely no sample code anywhere I could find.

                                      1)  There needs to be another bigger NSView that hold the total display.

                                      2) That a NSScrollView need to be added to the storyboard and the some how defined without using tag numbers.(I used view.subviews)

                                      3) That the NSView need to be added to the NSScrollView using ScrollView.documentView = NSView(in 1)

                                      I read the links you provided.