Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vurgunmert/552f40c7c4e5da3e17374a5897ed8223 to your computer and use it in GitHub Desktop.
Save vurgunmert/552f40c7c4e5da3e17374a5897ed8223 to your computer and use it in GitHub Desktop.
UIKit SegmentedControl ViewController
//
// UIKitSegmentedControlViewController.swift
// TemplateAppTvOS
//
// Created by Mert Vurgun on 21.08.2024.
//
import Foundation
import UIKit
class UIKitSegmentedControlViewController: UIViewController {
// MARK: 1. Create presentation - UISegmentedControl with items
private let segmentedControl = UISegmentedControl(items: ["Home", "Search", "Settings", "Move Up", "Move Center", "Move Down"])
// MARK: 2. Constraint references for updating the position of UISegmentedControl
private var segmentedControlTopConstraint: NSLayoutConstraint!
private var segmentedControlCenterConstraint: NSLayoutConstraint!
private var segmentedControlBottomConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
// MARK: 3. Set background color for the view
view.backgroundColor = .black
// MARK: 4. Setup and configure UISegmentedControl
setupSegmentedControl()
}
// MARK: 5. Configure UISegmentedControl with initial settings and constraints
private func setupSegmentedControl() {
view.addSubview(segmentedControl)
segmentedControl.selectedSegmentIndex = 0
// Set target-action for the UISegmentedControl's value change
segmentedControl.addTarget(self, action: #selector(segmentChanged), for: .valueChanged)
segmentedControl.translatesAutoresizingMaskIntoConstraints = false
// Initialize constraints for top and center positions
segmentedControlTopConstraint = segmentedControl.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
segmentedControlBottomConstraint = segmentedControl.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
segmentedControlCenterConstraint = segmentedControl.centerYAnchor.constraint(equalTo: view.centerYAnchor)
// MARK: 6. Position UISegmentedControl with initial constraints (centered)
NSLayoutConstraint.activate([
segmentedControlCenterConstraint,
segmentedControl.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])
}
// MARK: 7. Handle UISegmentedControl value change
@objc private func segmentChanged() {
switch segmentedControl.selectedSegmentIndex {
case 0:
print("Home selected")
case 1:
print("Search selected")
case 2:
print("Settings selected")
case 3:
moveSegmentedControlToTop()
case 4:
moveSegmentedControlToCenter()
case 5:
moveSegmentedControlToBottom()
default:
break
}
}
// MARK: 8. Move UISegmentedControl to the top of the screen
private func moveSegmentedControlToTop() {
// Deactivate center constraint and activate top constraint
segmentedControlCenterConstraint.isActive = false
segmentedControlTopConstraint.isActive = true
segmentedControlBottomConstraint.isActive = false
UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded()
}
}
// MARK: 9. Move UISegmentedControl to the center of the screen
private func moveSegmentedControlToCenter() {
// Deactivate top constraint and activate center constraint
segmentedControlTopConstraint.isActive = false
segmentedControlCenterConstraint.isActive = true
segmentedControlBottomConstraint.isActive = false
UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded()
}
}
// MARK: 10. Move UISegmentedControl to the bottom of the screen
private func moveSegmentedControlToBottom() {
// Deactivate top constraint and activate center constraint
segmentedControlTopConstraint.isActive = false
segmentedControlCenterConstraint.isActive = false
segmentedControlBottomConstraint.isActive = true
UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment