2 Replies
      Latest reply on Jun 3, 2019 5:28 AM by oreman
      oreman Level 1 Level 1 (0 points)

        I have a custom NSView that needs a horizontal and a vertical scroll bar.

         

        I've added the two scroll bars but both are always drawn as vertical scroll bars. Is there a property that I can set on the NSScroller to draw horizontally, or do I have to rotate the view?

        • Re: How do I create a horizontal NSScroller?
          Claude31 Level 8 Level 8 (6,595 points)

          Why not create directly an NSScrollView ?

          It is advised in Apple's doc:

          Typically, you don’t need to program with scrollers; instead, you configure them with an NSScrollView object

           

          If you still need to create programmatically, could you show how you created the scroll bars ?

          I have create a short demo code without problem, just setting the frame rectangle.

           

              @IBOutlet weak var viewWithScrollers: NSView!
          
                  let hScroll = NSScroller(frame: NSRect(x: 0, y: 0, width: 160, height: 12))
                  hScroll.scrollerStyle = .legacy
                  let vScroll = NSScroller(frame: NSRect(x: viewWithScrollers.frame.width - 12, y: 0, width: 12, height: 100))
                  vScroll.scrollerStyle = .overlay
                  viewWithScrollers.addSubview(vScroll)
                  viewWithScrollers.addSubview(hScroll)
            • Re: How do I create a horizontal NSScroller?
              oreman Level 1 Level 1 (0 points)

              Thanks, your example helped me to figure it out.

              The type of scroll-bar (horizontal or vertical) is determined by it's frame size when it is created.

               

              I've created my scroll-bars in my custom view's init() with the frame set to .zero (since my view does not have a size yet). The frame was adjusted when the view got layed out but at that time it is too late and it does not change.

               

              The fix was to use an arbitary frame size that is wider than it is taller in order to get a horizontal scroll bar, and taller than wider to get a vertical one.