Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI: how to get rid of Segmented Control text movement when an alert is shown?

In a small sample SwiftUI app, I have a settings view that shows a couple of option selections, implemented as segmented controls. The text in these segmented controls visibly moves when an alert is presented or dismissed. Is there a way to get rid of this glitch?

Paste this in a Playground to reproduce:

import SwiftUI
import PlaygroundSupport

struct FlickeringSegmentsView: View {
    @State var option = 0
    @State var alerting = false

    var body: some View {
        VStack(alignment: .center, spacing: 120) {
            Picker("options", selection: $option) {
                Text("Option A").tag(0)
                Text("Option B").tag(1)
            }
            .pickerStyle(SegmentedPickerStyle())
            .padding(16)

            Button(action: { self.alerting.toggle() },
                   label: { Text("Show Alert") }
            )
            .alert(isPresented: $alerting) {
                Alert(title: Text("Alert"))
            }
        }
    }
}

PlaygroundPage.current.setLiveView(FlickeringSegmentsView())
like image 525
Gereon Avatar asked Jan 03 '20 13:01

Gereon


1 Answers

This issue is resolved in Xcode 12 beta using the included iOS 14 simulator (and hopefully stays that way).

like image 92
Gereon Avatar answered Oct 31 '22 15:10

Gereon