SwiftUI-自动滚动水平滚动视图以显示全文标签

时间:2020-04-15 02:28:16

标签: ios swiftui scrollview

我有以下完美的代码。但是我无法解决的一个问题是,当Titles上的import SwiftUI struct CustomSegmentedPickerView: View { @State private var selectedIndex = 0 private var titles = ["Round Trip", "One Way", "Multi-City", "Other"] private var colors = [Color.red, Color.green, Color.blue, Color.yellow] @State private var frames = Array<CGRect>(repeating: .zero, count: 4) var body: some View { VStack { ScrollView(.horizontal, showsIndicators: false) { ZStack { HStack(spacing: 10) { ForEach(self.titles.indices, id: \.self) { index in Button(action: { self.selectedIndex = index }) { Text(self.titles[index]) }.padding(EdgeInsets(top: 16, leading: 20, bottom: 16, trailing: 20)).background( GeometryReader { geo in Color.clear.onAppear { self.setFrame(index: index, frame: geo.frame(in: .global)) } }) } } .background( Capsule().fill(self.colors[self.selectedIndex].opacity(0.4)) .frame(width: self.frames[self.selectedIndex].width, height: self.frames[self.selectedIndex].height, alignment: .topLeading) .offset(x: self.frames[self.selectedIndex].minX - self.frames[0].minX) , alignment: .leading ) } .animation(.default) .background(Capsule().stroke(Color.gray, lineWidth: 3)) Picker(selection: self.$selectedIndex, label: Text("What is your favorite color?")) { ForEach(0..<self.titles.count) { index in Text(self.titles[index]).tag(index) } }.pickerStyle(SegmentedPickerStyle()) Text("Value: \(self.titles[self.selectedIndex])") Spacer() } } } func setFrame(index: Int, frame: CGRect) { self.frames[index] = frame } } struct CustomSegmentedPickerView_Previews: PreviewProvider { static var previews: some View { CustomSegmentedPickerView() } } 在滚动视图中特别可见并且用户单击可见的文本部分时,我不仅要选择标题我希望scollview能够“自动滚动”,以便完整标题显示在滚动视图中,而只显示部分文本。

from tendo import singleton
try:
    me = singleton.SingleInstance()
except:
    print("Already running")
    sys.exit(-1)

0 个答案:

没有答案