Macos 如何像日历应用程序一样在触摸栏(mac OS)中创建日期选择器
我无法附加触摸栏的照片。但是,如果您启用了触摸栏,当您单击日历应用程序中的[Day Week Month Year]段时,您可以在触摸栏中看到不同类型的日期选择器。我必须创建一个相同的日期选择器。非常感谢您的帮助。 滚动月份可能是一个单独的NSScriber 在苹果的Ntouchbar目录中,有一些基于文本和基于图像的例子。请查看该项目中的scruberviewcontroller.m文件 下面是一些简化的代码,用于设置单个NSScriber来显示月份。您可能希望正确地本地化月份名称,并添加代码以突出显示当前选择Macos 如何像日历应用程序一样在触摸栏(mac OS)中创建日期选择器,macos,nstouchbar,Macos,Nstouchbar,我无法附加触摸栏的照片。但是,如果您启用了触摸栏,当您单击日历应用程序中的[Day Week Month Year]段时,您可以在触摸栏中看到不同类型的日期选择器。我必须创建一个相同的日期选择器。非常感谢您的帮助。 滚动月份可能是一个单独的NSScriber 在苹果的Ntouchbar目录中,有一些基于文本和基于图像的例子。请查看该项目中的scruberviewcontroller.m文件 下面是一些简化的代码,用于设置单个NSScriber来显示月份。您可能希望正确地本地化月份名称,并添加代码
static NSString *kMyScrubberID = @"com.mycompany.myapp.month.scrubber";
self.myTitlesArray = [NSArray arrayWithObjects:
@"Jan",
@"Feb",
@"Mar",
@"Apr",
@"May",
@"Jun",
@"Jul",
@"Aug",
@"Sep",
@"Oct",
@"Nov",
@"Dec",
nil];
// NSTouchBarDelegate
- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)anIDstr {
if ([anIDstr isEqualToString:kMyScrubberID]) {
NSCustomTouchBarItem *theScrubberItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:anIDstr];
NSScrubber *theScrubber = [[NSScrubber alloc] initWithFrame:NSMakeRect(0, 0, 300, 30)];
theScrubber.scrubberLayout = [[NSScrubberFlowLayout alloc] init];
[theScrubber registerClass:[NSScrubberTextItemView class] forItemIdentifier:anIDstr];
theScrubber.delegate = (id<NSScrubberDelegate>)self;
theScrubber.dataSource = (id<NSScrubberDataSource>)self;
theScrubber.mode = NSScrubberModeFree;
theScrubber.continuous = YES;
theScrubber.floatsSelectionViews = YES;
theScrubber.showsArrowButtons = NO;
theScrubber.selectionOverlayStyle = nil;
theScrubber.selectionBackgroundStyle = [NSScrubberSelectionStyle roundedBackgroundStyle];
theScrubber.backgroundColor = [NSColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
theScrubberItem.view = theScrubber;
return theScrubberItem;
} else {
return nil;
}
}
// NSScrubberDelegate
- (void)scrubber:(NSScrubber *)scrubber didSelectItemAtIndex:(NSInteger)selectedIndex {
NSLog(@"selected month = '%@'", [self.myTitlesArray objectAtIndex[selectedIndex]);
}
// NSScrubberDataSource
- (NSInteger)numberOfItemsForScrubber:(NSScrubber *)scrubber {
return [self.myTitlesArray count];
}
- (NSScrubberItemView *)scrubber:(NSScrubber *)scrubber viewForItemAtIndex:(NSInteger)index {
NSScrubberTextItemView *theItemView = [scrubber makeItemWithIdentifier:kMyScrubberID owner:nil];
if (index < [self.myTitlesArray count]) {
theItemView.textField.stringValue = [self.myTitlesArray objectAtIndex:index];
}
return theItemView;
}
// NSScrubberFlowLayoutDelegate
- (NSSize)scrubber:(NSScrubber *)scrubber layout:(NSScrubberFlowLayout *)layout sizeForItemAtIndex:(NSInteger)itemIndex {
return NSMakeSize(60, 30);
}
static NSString*kMyScrubberID=@“com.mycompany.myapp.month.scruber”;
self.myTitleArray=[NSArray arrayWithObjects:
@“简”,
@“二月”,
@“三月”,
@“四月”,
@“五月”,
@“六月”,
@“七月”,
@“八月”,
@“九月”,
@“十月”,
@“11月”,
@“十二月”,
零];
//NSTouchBarDelegate
-(可为空的NSTouchBar*)触摸栏:(NSTouchBar*)触摸栏makeItemForIdentifier:(NSTouchBarItemIdentifier)anIDstr{
if([anIDstr isEqualToString:kMyScrubberID]){
NSCustomTouchBarItem*scrubberitem=[[NSCustomTouchBarItem alloc]initWithIdentifier:anIDstr];
nsscruber*thescruber=[[nsscruber alloc]initWithFrame:NSMakeRect(0,0,300,30)];
scrubber.scruberLayout=[[nsscruberFlowLayout alloc]init];
[theScrubber注册表类:[NSScruberTextItemView类]for ItemIdentifier:anIDstr];
theScrubber.delegate=(id)self;
theScrubber.dataSource=(id)self;
scrubber.mode=nsscrubermodefree;
theScrubber.continuous=是;
scrubber.floatsSelectionViews=是;
scrubber.showsArrowButtons=否;
scrubber.selectionOverlayStyle=nil;
scrubber.selectionBackgroundStyle=[NSScruberselectionStyle roundedBackgroundStyle];
theScrubber.backgroundColor=[NSColor color withred:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
theScrubberItem.view=scrubber;
归还圣物;
}否则{
返回零;
}
}
//恩斯伯德尔盖特酒店
-(无效)洗涤器:(NS洗涤器*)洗涤器didSelectItemAtIndex:(NSInteger)selectedIndex{
NSLog(@“所选月份='%@'[self.myTitleArray objectAtIndex[selectedIndex]);
}
//NSScruberDataSource
-(NSInteger)洗涤器的项目数:(NSCLUSTER*)洗涤器{
返回[self.myTitlesArray count];
}
-(NSScruberItemView*)洗涤器:(NSScruber*)项目索引的洗涤器视图:(NSInteger)索引{
NSScruberTextItemView*IteemView=[Scruber makeItemWithIdentifier:kMyScrubberID所有者:nil];
如果(索引<[self.myTitlesArray count]){
IteemView.textField.stringValue=[self.MyTitleArray对象索引:索引];
}
返回视图;
}
//NSR低层布局委派
-(NSSize)洗涤器:(NSScriber*)洗涤器布局:(NSScriberFlowLayout*)布局尺寸标准索引:(NSInteger)项目索引{
返回NSMakeSize(60,30);
}
对于像日历这样的行为,选择停留在中间,项目移动,三个属性是关键的:模式=NSScruberModeFree
,连续=是
和浮动选择视图=是
。