TTBaseUISegmentedControl
CoreEnhanced UISegmentedControl subclass with 2 visual styles: a pill-shaped DEFAULT (full-color selection) and a modern tab-style LINE_BOTTOM with animated underline indicator. Both styles pick colors from your ViewConfig theme automatically.
🎨 Styles
public enum TYPE {
case DEFAULT // Pill-shaped, filled selected segment
case LINE_BOTTOM // Flat tab bar with animated bottom underline
}
| Type | Background | Selected | Use Case |
|---|---|---|---|
.DEFAULT | segBgDef | Filled pill in segSelectedColor | Filter bars, binary choices |
.LINE_BOTTOM | segBgLine | Animated underline in lineBottomColor | Tab navigation, category switching |
🚀 Usage
Default Pill Style
let filterSeg = TTBaseUISegmentedControl(
with: .DEFAULT,
items: ["All", "Active", "Completed"]
)
filterSeg.onTouchHandler = { seg, index in
switch index {
case 0: viewModel.filter = .all
case 1: viewModel.filter = .active
case 2: viewModel.filter = .completed
default: break
}
}
Line Bottom Tab Style
let tabSeg = TTBaseUISegmentedControl(
with: .LINE_BOTTOM,
items: ["Overview", "Details", "Reviews"]
)
tabSeg.setHeightAnchor(constant: TTSize.H_SEG)
tabSeg.onTouchHandler = { seg, index in
pageViewController.scrollToPage(index)
}
Programmatic Selection
// Select index 2 and animate underline (LINE_BOTTOM only)
tabSeg.setSelectedIndex(with: 2)
// Update underline position (call after frame changes)
tabSeg.changeUnderlinePosition()
Customization via Subclass
class MySegControl: TTBaseUISegmentedControl {
// Override computed properties to customize colors
override var selectedColor: UIColor { .systemPurple }
override var textColor: UIColor { .secondaryLabel }
override var textSelectedColor: UIColor { .white }
override var lineBottomColor: UIColor { .systemPurple }
override var lineBottomHeight: CGFloat { 3 }
override var fontSize: CGFloat { 15 }
override func updateUI() {
// Additional setup
}
}
📖 API Reference
| Method / Property | Type | Description |
|---|---|---|
onTouchHandler | Closure? | Called with (seg, selectedIndex) on tap |
setSelectedIndex(with:) | Void | Programmatic selection + animate underline |
changeUnderlinePosition() | Void | Recalculate underline position (after frame changes) |
underlineView | UIView | The underline indicator view (LINE_BOTTOM only) |
selectedColor | UIColor | Selected segment tint (override in subclass) |
textColor | UIColor | Normal segment text color |
textSelectedColor | UIColor | Selected segment text color |
lineBottomHeight | CGFloat | Underline thickness (LINE_BOTTOM) |
lineBottomColor | UIColor | Underline color (LINE_BOTTOM) |
fontSize | CGFloat | Segment title font size |
Pro Tip: For
.LINE_BOTTOM used with a paged scroll view, call changeUnderlinePosition() in the scroll delegate's scrollViewDidScroll for perfectly synced tab animations.