【问题标题】:Overlay NSScroller over content在内容上覆盖 NSScroller
【发布时间】:2010-11-21 02:55:48
【问题描述】:

有没有办法将 NSScroller 覆盖 滚动视图的内容(如在 iOS 中)?我已经尝试了几种方法:

a)设置滚动视图内容视图(NSClipView)的框架以延伸到滚动条的边界

b) 添加一个 NSScroller 对象作为滚动视图的子视图(定位在我想要的位置)

c) 创建一个完全自定义的滚动视图并将其放置为子视图(这可行,但这意味着我需要重写 NSScroller 的所有功能)

Sparrow 似乎成功地做到了这一点,它似乎是通过一个常规的 NSScroller 子类来做到的(因为它响应了在系统偏好设置>>外观中设置的滚动设置)。问题并不是真正绘制滚动条,而是让它覆盖内容。

感谢任何建议:-)

【问题讨论】:

    标签: objective-c cocoa nsview nsscrollview nsscroller


    【解决方案1】:

    Here's where you can set the custom class of your scrollbars.

    之后,通过覆盖NSScrollView-tile 方法,您将正确放置它们。

    【讨论】:

      【解决方案2】:

      这是我的解决方案: 创建一个扩展 NSScroller 的 MyScroller 类

      在 MyScroller.m 中:

      #import "MyScroller.h"
      
      
      @implementation MyScroller
      
      +(CGFloat) scrollerWidth{
          return 10;
      }
      
      +(CGFloat) scrollerWidthForControlSize:(NSControlSize)controlSize{
          return 10;
      }
      
      - (void) drawBackground:(NSRect) rect{
          NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:0 yRadius:0];
          [[NSColor whiteColor] set];
          [path fill];
      }
      
      - (void)drawKnob{
          [self drawBackground:[self rectForPart:0]];
          [self drawBackground:[self rectForPart:1]];
          [self drawBackground:[self rectForPart:2]];
          [self drawBackground:[self rectForPart:4]];
          [self drawBackground:[self rectForPart:5]];
          [self drawBackground:[self rectForPart:6]];
      
      
          NSRect knobRect = [self rectForPart:NSScrollerKnob];
          NSRect newRect = NSMakeRect((knobRect.size.width - [MyScroller scrollerWidth]) / 2, knobRect.origin.y, [MyScroller scrollerWidth], knobRect.size.height);
          NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:newRect xRadius:5 yRadius:5];
          [[NSColor grayColor] set];
          [path fill];
      }
      @end
      

      然后只需在 Interface Builder 中为 Scroller 设置自定义类。

      【讨论】:

      • 使用此解决方案无法通过相关属性自动隐藏滚动条。
      【解决方案3】:

      我最近发布了 RFOverlayScrollView,应该可以解决您的问题:

      来源:https://github.com/rheinfabrik/RFOverlayScrollView

      博文:http://blog.rheinfabrik.de/blog/2013/01/01/introducing-rfoverlayscrollview/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-30
        • 1970-01-01
        • 1970-01-01
        • 2012-05-30
        • 2018-03-30
        • 2014-04-27
        相关资源
        最近更新 更多