Hide scrollers while leaving scrolling itself enabled in NSScrollView

荒凉一梦 提交于 2019-12-04 00:34:06

问题


I'd like to hide the NSScrollers of NSScrollView. I'm aware of the methods setHasHorizontalScroller: and setHasVerticalScroller:. However, if I disable the scrollers, scrolling in that direction gets disabled as well. I'd like to only hide the scrollers while maintaining the possibility to scroll. Any thoughts on this?


回答1:


I was able to do this by doing:

[labelsScrollView setHasHorizontalScroller:YES];
[[labelsScrollView horizontalScroller] setAlphaValue:0];



回答2:


The trick of setting alphaValue to zero has issue as invisible scroller still receives touches. Here is what we did in order to solve this (Swift 4).

class InvisibleScroller: NSScroller {

   override class var isCompatibleWithOverlayScrollers: Bool {
      return true
   }

   override class func scrollerWidth(for controlSize: NSControl.ControlSize, scrollerStyle: NSScroller.Style) -> CGFloat {
      return CGFloat.leastNormalMagnitude // Dimension of scroller is equal to `FLT_MIN`
   }

   public override init(frame frameRect: NSRect) {
      super.init(frame: frameRect)
      setupUI()
   }

   private func setupUI() {
      // Below assignments not really needed, but why not.
      scrollerStyle = .overlay
      alphaValue = 0
   }
}

Usage:

private class TabBarScrollView: NSScrollView {

   private func setupUI() {

      borderType = .noBorder
      backgroundColor = .clear
      drawsBackground = false

      horizontalScrollElasticity = .none
      verticalScrollElasticity = .none

      automaticallyAdjustsContentInsets = false
      horizontalScroller = InvisibleScroller()
   }
}



回答3:


if collectionView.enclosingScrollView?.hasHorizontalScroller == true {
        collectionView.enclosingScrollView?.horizontalScroller?.scrollerStyle = .overlay
        collectionView.enclosingScrollView?.horizontalScroller?.alphaValue = 0.0
    } else {
        print("horizontalScroller")
    }

Make sure horizontal scroller is selected:




回答4:


Have you checked the NSScrollView Class Reference ?

[scrollView setHasVerticalScroller:NO];

this method doesn't disable scrolling.

If you still do not want to use that method you can also:

[scrollView setHorizontalScroller:nil];



回答5:


I'm looking for an answer to this as well since setting hasVerticalScroller = false disables trackpad scrolling for me. In the meantime, my horrible hack is to use the following in my NSScrollView subclass:

self.hasVerticalScroller = true
self.scrollerInsets.right = -999999

The scroller is technically visible, it is just way off the edge of the view. :(




回答6:


This is my solution from sam's answer above:

[myNSScrollView setHasVerticalScroller:YES];
[_myNSScrollView setScrollerInsets:NSEdgeInsetsMake(0, 0, 0, -99999)];

The scroller will be hidden even during the scrolling process.



来源:https://stackoverflow.com/questions/9364953/hide-scrollers-while-leaving-scrolling-itself-enabled-in-nsscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!