【问题标题】:SwiftUI Create a Custom Segmented Control also in a ScrollViewSwiftUI 在 ScrollView 中创建自定义分段控件
【发布时间】:2020-07-03 09:10:19
【问题描述】:

下面是我创建标准分段控件的代码。

struct ContentView: View {

    @State private var favoriteColor = 0
    var colors = ["Red", "Green", "Blue"]

    var body: some View {
        VStack {
            Picker(selection: $favoriteColor, label: Text("What is your favorite color?")) {
                ForEach(0..<colors.count) { index in
                    Text(self.colors[index]).tag(index)
                }
            }.pickerStyle(SegmentedPickerStyle())

            Text("Value: \(colors[favoriteColor])")
        }
    }
}

我的问题是如何修改它以拥有一个自定义的分段控件,我可以让边框与我自己的颜色一起四舍五入,因为使用 UIKit 很容易做到这一点?有没有人这样做过。

我的最佳示例是 Uber Eats 应用程序,当您选择一家餐厅时,您可以通过在自定义分段控件中选择一个选项来滚动到菜单的特定部分。

包括我希望自定义的元素:

* 更新 *

最终设计图

【问题讨论】:

  • 对于自定义段控制,您可以使用UIViewRepresentable 协议实现它
  • @Mac3n 这没有多大帮助
  • 您可以添加一张图片来显示您希望选择器的外观吗?
  • @LuLuGaGa 我用最终产品的图片更新了问题
  • @LuLuGaGa 我能问一个问题吗,我把它放在一个水平滚动视图中,你是否知道我可以如何调整偏移量,如果用户选择一个选择半部分,则所选段不会被截断剪掉了吗?

标签: ios swiftui segmentedcontrol


【解决方案1】:

这就是你要找的吗?

import SwiftUI

struct CustomSegmentedPickerView: View {
  @State private var selectedIndex = 0
  private var titles = ["Round Trip", "One Way", "Multi-City"]
  private var colors = [Color.red, Color.green, Color.blue]
  @State private var frames = Array<CGRect>(repeating: .zero, count: 3)

  var body: some View {
    VStack {
      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()
  }
}

【讨论】:

  • 这正是我所指的。询问如果我需要在水平滚动视图中使用它怎么办,因为我有比我提到的三个选项更多的选项,并且我想跨越屏幕大小。
  • 视图 --kinda-- 当你包裹在一个水平的 ScrollView 中时仍然有效。但它当然不会调整滚动位置,这使得它不能完全有用。也许你可以从这篇文章中收集一些关于如何做到这一点的想法:stackoverflow.com/questions/57258846/…
  • 滚动水平滚动视图:实际上我刚刚偶然发现了来自同一用户的另一个 SO 帖子:stackoverflow.com/questions/60855852/…
  • 惊人的答案。我会用.easeInOut(duration: 0.2) 替换默认动画,让它看起来更类似于 Picker 动画
  • 对于以后遇到这种情况的人来说,因为视图的测量只在onAppear上进行,如果布局发生变化,它将停止运行。解决方案:stackoverflow.com/a/66512870/560942
【解决方案2】:

如果我正确地关注问题,那么起点可能类似于下面的代码。显然,造型需要一点关注。这对段具有硬连线宽度。为了更加灵活,您需要使用 Geometry Reader 来测量可用的内容并划分空间。

struct ContentView: View {

      @State var selection = 0

      var body: some View {

            let item1 = SegmentItem(title: "Some Way", color: Color.blue, selectionIndex: 0)
            let item2 = SegmentItem(title: "Round Zip", color: Color.red, selectionIndex: 1)
            let item3 = SegmentItem(title: "Multi-City", color: Color.green, selectionIndex: 2)

            return VStack() {
                  Spacer()
                  Text("Selected Item: \(selection)")
                  SegmentControl(selection: $selection, items: [item1, item2, item3])
                  Spacer()
            }
      }
}


struct SegmentControl : View {

      @Binding var selection : Int
      var items : [SegmentItem]

      var body : some View {

            let width : CGFloat = 110.0

            return HStack(spacing: 5) {
                  ForEach (items, id: \.self) { item in
                        SegmentButton(text: item.title, width: width, color: item.color, selectionIndex: item.selectionIndex, selection: self.$selection)
                  }
            }.font(.body)
                  .padding(5)
                  .background(Color.gray)
                  .cornerRadius(10.0)
      }
}


struct SegmentButton : View {

      var text : String
      var width : CGFloat
      var color : Color
      var selectionIndex = 0
      @Binding var selection : Int

      var body : some View {
            let label = Text(text)
                  .padding(5)
                  .frame(width: width)
                  .background(color).opacity(selection == selectionIndex ? 1.0 : 0.5)
                  .cornerRadius(10.0)
                  .foregroundColor(Color.white)
                  .font(Font.body.weight(selection == selectionIndex ? .bold : .regular))

            return Button(action: { self.selection = self.selectionIndex }) { label }
      }
}


struct SegmentItem : Hashable {
      var title : String = ""
      var color : Color = Color.white
      var selectionIndex = 0
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

【讨论】:

  • 感谢您的宝贵时间,但上述答案已失效
  • 另一个答案真的很好,尤其是它再现选择器动画的方式。
【解决方案3】:

上述解决方案都不适合我,因为 GeometryReader 一旦放置在导航视图中就会返回不同的值,这会导致活动指示器在后台的定位失效。我找到了替代解决方案,但它们仅适用于固定长度的菜单字符串。也许有一个简单的修改可以使上述代码贡献工作,如果是这样,我会渴望阅读它。如果您遇到与我相同的问题,那么这可能对您有用。

感谢 Reddit 用户“End3r117”和这篇 SwiftWithMajid 文章 https://swiftwithmajid.com/2020/01/15/the-magic-of-view-preferences-in-swiftui/ 的启发,我能够制定解决方案。这适用于 NavigationView 内部或外部,并接受各种长度的菜单项。

struct SegmentMenuPicker: View {
    var titles: [String]
    var color: Color
    
    @State private var selectedIndex = 0
    @State private var frames = Array<CGRect>(repeating: .zero, count: 5)

    var body: some View {
        VStack {
            ZStack {
                HStack(spacing: 10) {
                    ForEach(self.titles.indices, id: \.self) { index in
                        Button(action: {
                            print("button\(index) pressed")
                            self.selectedIndex = index
                        }) {
                            Text(self.titles[index])
                                .foregroundColor(color)
                                .font(.footnote)
                                .fontWeight(.semibold)
                        }
                        .padding(EdgeInsets(top: 0, leading: 5, bottom: 0, trailing: 5))
                        .modifier(FrameModifier())
                        .onPreferenceChange(FramePreferenceKey.self) { self.frames[index] = $0 }
                    }
                }
                .background(
                    Rectangle()
                        .fill(self.color.opacity(0.4))
                        .frame(
                            width: self.frames[self.selectedIndex].width,
                            height: 2,
                            alignment: .topLeading)
                        .offset(x: self.frames[self.selectedIndex].minX - self.frames[0].minX, y: self.frames[self.selectedIndex].height)
                    , alignment: .leading
                )
            }
            .padding(.bottom, 15)
            .animation(.easeIn(duration: 0.2))

            Text("Value: \(self.titles[self.selectedIndex])")
            Spacer()
        }
    }
}

struct FramePreferenceKey: PreferenceKey {
    static var defaultValue: CGRect = .zero

    static func reduce(value: inout CGRect, nextValue: () -> CGRect) {
        value = nextValue()
    }
}

struct FrameModifier: ViewModifier {
    private var sizeView: some View {
        GeometryReader { geometry in
            Color.clear.preference(key: FramePreferenceKey.self, value: geometry.frame(in: .global))
        }
    }

    func body(content: Content) -> some View {
        content.background(sizeView)
    }
}

struct NewPicker_Previews: PreviewProvider {
    static var previews: some View {
        VStack {
            SegmentMenuPicker(titles: ["SuperLongValue", "1", "2", "Medium", "AnotherSuper"], color: Color.blue)
            NavigationView {
                SegmentMenuPicker(titles: ["SuperLongValue", "1", "2", "Medium", "AnotherSuper"], color: Color.red)
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多