TTBaseUISegmentedControl

Core

Enhanced 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
}
TypeBackgroundSelectedUse Case
.DEFAULTsegBgDefFilled pill in segSelectedColorFilter bars, binary choices
.LINE_BOTTOMsegBgLineAnimated underline in lineBottomColorTab 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 / PropertyTypeDescription
onTouchHandlerClosure?Called with (seg, selectedIndex) on tap
setSelectedIndex(with:)VoidProgrammatic selection + animate underline
changeUnderlinePosition()VoidRecalculate underline position (after frame changes)
underlineViewUIViewThe underline indicator view (LINE_BOTTOM only)
selectedColorUIColorSelected segment tint (override in subclass)
textColorUIColorNormal segment text color
textSelectedColorUIColorSelected segment text color
lineBottomHeightCGFloatUnderline thickness (LINE_BOTTOM)
lineBottomColorUIColorUnderline color (LINE_BOTTOM)
fontSizeCGFloatSegment 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.