如何在触摸栏(mac OS)中创建日期选择器,如日历应用程序

时间:2016-11-09 08:55:00

标签: macos nstouchbar

我无法附上触摸条的照片。但是,如果您启用了触控栏,当您单击日历应用程序中的[日周月份年]段时,您可以在触摸栏中看到不同类型的日期选择器。我必须创建一个相同的日期选择器。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

enter image description here

滚动月份可能是单个NSScrubber。

Apple的NSTouchBar目录source中有一些基于文本和基于图像的示例。看看该项目中的ScrubberViewController.m文件。

下面是一些设置单个NSScrubber以显示月份的简化代码。您可能希望正确地本地化月份名称,并添加代码以突出显示当前选择。

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);
}