1

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

4

1 回答 1

3

在此处输入图像描述

滚动月份可能是单个 NSScrubber。

Apple 的 NSTouchBar 目录源中有一些基于文本和基于图像的示例。查看该项目中的 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);
}
于 2016-11-12T22:13:08.297 回答