I am trying to set the height of the segmented control to be higher than the default. I am not too familiar with UIKit so I am having a hard time getting it to work. I am using the picker in SwiftUI via a representable. Here is my code...
struct SegmentedControl: UIViewRepresentable {
typealias UIViewType = UISegmentedControl
var items: [String]
@Binding var selectedSegmentIndex: Int
func makeUIView(context: Context) -> UISegmentedControl {
let segmentedControl = UISegmentedControl(items: items)
segmentedControl.frame.size.height = 50.0
segmentedControl.selectedSegmentIndex = selectedSegmentIndex
segmentedControl.addTarget(context.coordinator, action: #selector(Coordinator.valueChanged(_:)), for: .valueChanged)
let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UISegmentedControl.appearance().setTitleTextAttributes(titleTextAttributes, for: .selected)
UISegmentedControl.appearance().selectedSegmentTintColor = .systemBlue
return segmentedControl
}
func updateUIView(_ uiView: UISegmentedControl, context: Context) {
uiView.selectedSegmentIndex = selectedSegmentIndex
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject {
var control: SegmentedControl
init(_ control: SegmentedControl) {
self.control = control
}
@objc func valueChanged(_ sender: UISegmentedControl) {
control.selectedSegmentIndex = sender.selectedSegmentIndex
}
}
}
Here is where I show the picker representable in SwiftUI
struct TestingView: View {
enum ProfileSection : String, CaseIterable {
case tweets = "Tweets"
case media = "Media"
case likes = "Likes"
}
@State var segmentationSelection : ProfileSection = .tweets
var body: some View {
SegmentedControl(items: ProfileSection.allCases.map { $0.rawValue }, selectedSegmentIndex: Binding(
get: { self.segmentationSelection.rawValue.hashValue },
set: { newIndex in
if let newSelection = ProfileSection.allCases.first(where: { $0.rawValue.hashValue == newIndex }) {
self.segmentationSelection = newSelection
}
}
))
}
}