SwiftUI: 如何在显示警告时消除分段控件文本移动?

huangapple go评论69阅读模式
英文:

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

问题

I can help with the translation of the code part. Here it is:

在一个小的示例 SwiftUI 应用中,我有一个设置视图,显示了一些选项选择,实现为分段控件。当显示或取消警报时,这些分段控件中的文本会明显移动。是否有方法消除这个问题?

将以下内容粘贴到 Playground 中以重现:

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("选项", selection: $option) {
                Text("选项 A").tag(0)
                Text("选项 B").tag(1)
            }
            .pickerStyle(SegmentedPickerStyle())
            .padding(16)

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

PlaygroundPage.current.setLiveView(FlickeringSegmentsView())

Please note that I've translated the code, but if you have any questions or need further assistance, feel free to ask.

英文:

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())

答案1

得分: 2

这个问题在 Xcode 12 beta 中得到解决,使用了包含的 iOS 14 模拟器(希望保持这种状态)。

英文:

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

答案2

得分: 0

Sure, here's the translated code you provided:

public protocol SegmentedPickerViewElementTraits: Hashable {
    var localizedText: String { get }
}

public struct SegmentedPickerView<Value, Data, ID, Label>: View
    where
    Value: SegmentedPickerViewElementTraits,
    Data: RandomAccessCollection,
    Data.Element == Value,
    ID: Hashable,
    Label: View {

    public let data: Data
    public let id: KeyPath<Data.Element, ID>
    public let selection: Binding<Value>
    public let label: Label

    public init(data: Data,
                id: KeyPath<Data.Element, ID>,
                selection: Binding<Value>,
                label: Label) {
        self.data = data
        self.id = id
        self.selection = selection
        self.label = label
    }

    public var body: some View {
        Picker(selection: selection, label: label) {
            ForEach(data, id: id) {
                Text($0.localizedText).tag($0)
            }
        }
        .pickerStyle(SegmentedPickerStyle())
    }
}
enum Options: UInt8, CaseIterable {
    case optionA
    case optionB
}

extension Options: SegmentedPickerViewElementTraits {
    var localizedText: String {
        switch self {
        case .optionA:
            return "Option A"
        case .optionB:
            return "Option B"
        }
    }
}

struct FlickeringSegmentsView: View {
    @State
    var option: Options = .optionA

    @State
    var alerting = false

    var body: some View {
        VStack(alignment: .center, spacing: 120) {
            SegmentedPickerView(
                data: Options.allCases,
                id: \.self,
                selection: $option,
                label: Text("options")
            )
            .padding(16)

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

Please note that I've removed the HTML entity codes (&lt; and &quot;) for better readability.

英文:

I hope code below should help you:

public protocol SegmentedPickerViewElementTraits: Hashable {
    var localizedText: String { get }
}

public struct SegmentedPickerView&lt;Value, Data, ID, Label&gt;: View
    where
    Value: SegmentedPickerViewElementTraits,
    Data: RandomAccessCollection,
    Data.Element == Value,
    ID: Hashable,
    Label: View {

    public let data: Data
    public let id: KeyPath&lt;Data.Element, ID&gt;
    public let selection: Binding&lt;Value&gt;
    public let label: Label

    public init(data: Data,
                id: KeyPath&lt;Data.Element, ID&gt;,
                selection: Binding&lt;Value&gt;,
                label: Label) {
        self.data = data
        self.id = id
        self.selection = selection
        self.label = label
    }

    public var body: some View {
        Picker(selection: selection, label: label) {
            ForEach(data, id: id) {
                Text($0.localizedText).tag($0)
            }
        }
        .pickerStyle(SegmentedPickerStyle())
    }
}

and lets modify your code:

enum Options: UInt8, CaseIterable {
    case optionA
    case optionB
}

extension Options: SegmentedPickerViewElementTraits {
    var localizedText: String {
        switch self {
        case .optionA:
            return &quot;Option A&quot;
        case .optionB:
            return &quot;Option B&quot;
        }
    }
}

struct FlickeringSegmentsView: View {
    @State
    var option: Options = .optionA

    @State
    var alerting = false

    var body: some View {
        VStack(alignment: .center, spacing: 120) {
            SegmentedPickerView(
                data: Options.allCases,
                id: \.self,
                selection: $option,
                label: Text(&quot;options&quot;)
            )
            .padding(16)

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

huangapple
  • 本文由 发表于 2020年1月3日 21:14:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定