Segmented Control 分段控件 macos

标本可交互 —— 点点看。Specimen is live — try it.

Anatomy — every part, named解剖 —— 每个部件的名字

  1. 1 Selected segment选中分段 NSSegmentedControl.selectedSegment

    “The section that stays pressed” is the selected segment.

    “一直保持按下的那一格”就是选中分段。

    Prompt fragmentPrompt 片段

    the NSSegmentedControl.selectedSegment with the native persistent selected fill

    带原生常驻选中填充的 NSSegmentedControl.selectedSegment

  2. 2 Segment divider分段分隔线 NSSegmentedControl

    “The little line separating two choices in the pill” is a segment divider.

    “胶囊里隔开两个选项的细线”就是分段分隔线。

    Prompt fragmentPrompt 片段

    the native divider between adjacent choices inside one NSSegmentedControl

    同一个 NSSegmentedControl 内部相邻选项之间的原生分隔线

Prompt — paste into your agentPrompt —— 直接粘贴给你的代理

Use a Segmented Control with NSSegmentedControl (SwiftUI: a Picker with PickerStyle.segmented), showing the active segment with the native selected treatment. Keep the buttons connected as one control and bind selection so exactly one segment remains selected for this view switcher.

使用分段控件:NSSegmentedControl(SwiftUI:带 PickerStyle.segmented 的 Picker),用原生选中样式标示当前分段。让各按钮连成一个整体控件,并绑定选中状态,保证这个视图切换器始终恰好有一个分段被选中。

Debug prompt — when it misbehaves调试 Prompt —— 当它不听话时

Debug my macOS segmented control (NSSegmentedControl). Rule out: trackingMode wrong — momentary segments fire and deselect, selectOne keeps a selection; selectedSegment being -1 in the action for momentary mode; segment images not template so dark mode breaks; auto segment widths jumping when labels change. The symptom:

调试我的 macOS 分段控件(NSSegmentedControl)。逐一排除:trackingMode 设错——momentary 模式的分段触发后立刻取消选中,selectOne 才会保留选中;momentary 模式下 action 里 selectedSegment 为 -1;分段图片不是模板图像导致深色模式显示异常;标签变化时自动分段宽度跳动。症状是:

In code代码里叫什么

Framework框架Symbol符号Note说明
AppKit NSSegmentedControl
SwiftUI PickerStyle.segmented
AppKit NSSegmentedControl.trackingMode single, multiple, or momentary selection behavior单选、多选或瞬时选择行为
SwiftUI View.pickerStyle(_:)

See also相关词条