我想在我的应用的触控栏中显示一个可滚动的按钮列表,类似于 Safari 在输入地址时显示收藏夹的方式。
滚动视图似乎不是标准的 TouchBar 控件之一。您为此使用哪些控件?
我想在我的应用的触控栏中显示一个可滚动的按钮列表,类似于 Safari 在输入地址时显示收藏夹的方式。
滚动视图似乎不是标准的 TouchBar 控件之一。您为此使用哪些控件?
NSScrollView实际上在 TouchBar ( https://developer.apple.com/reference/appkit/nstouchbar#2587738 ) 上受支持。
NSScrubber 旨在成为项目的选择器,类似于分段控件,但具有可滚动列表的额外好处。NSScrubber 也管理它自己的高亮状态并且会吃掉触摸事件。您可以通过将它们弹出到滚动视图中来获得 NSButtons 的水平可滚动列表。这是一个使用约束创建 NSCustomTouchBarItem 的快速示例:
NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame:CGRectMake(0, 0, 400, 30)];
NSMutableDictionary *constraintViews = [NSMutableDictionary dictionary];
NSView *documentView = [[NSView alloc] initWithFrame:NSZeroRect];
NSString *layoutFormat = @"H:|-8-";
NSSize size = NSMakeSize(8, 30);
for (int i = 0; i <= 8; i++) {
NSString *objectName = [NSString stringWithFormat:@"button%@",@(i)];
NSButton *button = [NSButton buttonWithTitle:objectName
target:nil
action:nil];
button.translatesAutoresizingMaskIntoConstraints = NO;
[documentView addSubview:button];
// Constraint information
layoutFormat = [layoutFormat stringByAppendingString:[NSString stringWithFormat:@"[%@]-8-", objectName]];
[constraintViews setObject:button forKey:objectName];
size.width += 8 + button.intrinsicContentSize.width;
}
layoutFormat = [layoutFormat stringByAppendingString:[NSString stringWithFormat:@"|"]];
NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:layoutFormat
options:NSLayoutFormatAlignAllCenterY
metrics:nil
views:constraintViews];
[documentView setFrame:NSMakeRect(0, 0, size.width, size.height)];
[NSLayoutConstraint activateConstraints:hConstraints];
scrollView.documentView = documentView;
NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:@"com.TouchBar.scrollButtons"];
item.view = scrollView;
您可能知道,您将NSTouchBarItem
s 添加到TouchBar
实例中以便将项目添加到 Touch Bar。
为了添加一个可滚动列表,创建一个NSScrubber
. 设置完成后,您需要将其添加NSScrubber
到 的自定义实例中NSCustomTouchBarItem
,最有可能添加到view
属性中。然后,您可以将该项目添加到触控栏。请务必阅读链接的文档。
我刚刚将代码从 Santiago Gil 翻译成 Swift 4。
let scrollView = NSScrollView(frame: CGRect(x: 0, y: 0, width: 400, height: 30))
var constraintViews: Dictionary<String, Any> = [:]
let documentView = NSView(frame: NSZeroRect)
var layoutFormat: String = "H:|-8-"
var size = NSSize(width: 8, height: 30)
for i in 0...8 {
let objectName: String = "button\(i)"
let button: NSButton = NSButton(title: objectName, target: nil, action: nil)
button.translatesAutoresizingMaskIntoConstraints = false
documentView.addSubview(button)
// Constraint Information
layoutFormat += "[\(objectName)]-8-"
constraintViews[objectName] = button
size.width += 8 + button.intrinsicContentSize.width
}
layoutFormat += "|"
let hConstraints: Array<NSLayoutConstraint> = NSLayoutConstraint.constraints(withVisualFormat: layoutFormat, options: .alignAllCenterY, metrics: nil, views: constraintViews)
documentView.frame = NSRect(x: 0, y: 0, width: size.width, height: size.height)
NSLayoutConstraint.activate(hConstraints)
scrollView.documentView = documentView
let item = NSCustomTouchBarItem(identifier: .controlScrubber)
item.view = scrollView
希望这可以帮助某人;-)