下面是我创建一个标准分段控件的代码。
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应用程序,当你选择一家餐厅时,你可以通过在定制的分段控件中选择一个选项滚动到菜单的特定部分。
包括我期待定制的元素:
更新
最终设计图片
4条答案
按热度按时间6jygbczu1#
这是你要找的吗?
n3ipq98p2#
如果我没理解错的话,起点可能类似于下面的代码。造型,显然需要一点注意。这具有段的硬连线宽度。为了更灵活,你需要使用几何阅读器来测量可用的空间并划分空间。
zpgglvta3#
上述解决方案对我来说都不起作用,因为GeometryReader一旦放置在导航视图中就会返回不同的值,这会抛出活动指示器在背景中的定位。我找到了其他的解决方案,但它们只适用于固定长度的菜单字符串。也许有一个简单的修改可以使上面的代码工作,如果是的话,我会迫不及待地阅读它。如果你和我有同样的问题,那么这可能会为你工作。
多亏了Reddit用户“End3r117”的灵感和这篇SwiftWithMajid文章https://swiftwithmajid.com/2020/01/15/the-magic-of-view-preferences-in-swiftui/,我能够制定一个解决方案。它可以在NavigationView内部或外部工作,并接受各种长度的菜单项。
ttvkxqim4#
由于上面没有任何解决方案可以解决本地解决方案的感觉,我基于上面的实现创建了自己的解决方案。https://github.com/poromaa/swiftui-capsule-picker/tree/main