修改NSScrollView滾動條的顔色
項目中需要修改滾動條的顔色,查閱自己資料,自己總結,可以按照如下方式實作:
首先自定義滾動條,CustomScroller.h檔案
#import <Cocoa/Cocoa.h>
@interface CustomScroller : NSScroller
@end
CustomScroller.m檔案
#import "CustomScroller.h"
@implementation CustomScroller
- (void)drawKnob {
[super drawKnob];
NSRect knobRect = NSInsetRect([self rectForPart:NSScrollerKnob], 3, 0);
NSBezierPath *bezierPath = [NSBezierPath bezierPathWithRoundedRect:knobRect xRadius:3 yRadius:3];
[[NSColor redColor] set];
[bezierPath fill];
}
@end
經過上述步驟,項目中使用Scroller
tableContainerView.verticalScroller = [[CustomScroller alloc] init];
最終顔色是變了,但是,滾動條不消失。
最終引用RFOverlayScrollView實作,但是它這個不能修改顔色,是以将上述
- (void)drawKnob
加入RFOverlayScroller類中即可實作修改滾動條顔色功能。細節如顔色、圓角、寬度等可根據個人需要調整,總體思路就是這樣
以下是我想送出到Stack Overflow網站的回答,可惜由于以前回答不認真,賬号被封,送出不了,索性送出給咱們CSDN用吧。
I need to set scroller color with red, I achieved it by follow:
- write a subclass of NSScroller
- override drawKnob
-(void)drawKnob {
[super drawKnob];
NSRect knobRect = NSInsetRect([self rectForPart:NSScrollerKnob], 3, 0);
NSBezierPath *bezierPath = [NSBezierPath bezierPathWithRoundedRect:knobRect xRadius:3 yRadius:3];
[[NSColor redColor] set];
[bezierPath fill];
}
But I found scroller will never disappear, finally I used RFOverlayScrollView and add these code to class of RFOverlayScroller resolved it finally, hope it is helpful for you!