Is there a way to fix the incomplete separator in the selected row? the issue is in simulator and real device.
Xcode 12.4 and iOS 14.4
struct ContentView: View {
var numbers = ["1", "2", "3", "4"]
#State private var selectedIndex = 0
var body: some View {
NavigationView {
Form {
Section {
Picker(selection: $selectedIndex, label: Text("Test")) {
ForEach(0 ..< numbers.count) {
Text(self.numbers[$0])
}
}
}
}
}
}
}
Related
I'm using .searchable to embed a search bar into a list view. However, when the .searchable is nested in a NavigationStack (iOS 16 API), it is twinking when the page is loaded (shows up at first and disappears quickly). I hope both pages have a searchable feature.
I can reproduce this issue both on my device iPhone 12 and the simulator iPhone 14. Am I putting the modifier in an incorrect place?
struct ContentView: View {
#State private var selection = "2"
#State var items: [String] = ["0", "1", "2", "3", "4"]
#State var searchText = ""
var body: some View {
NavigationStack {
List {
ForEach(items, id: \.self) { item in
NavigationLink {
NestedListView(items: items)
} label: {
Text(item)
}
}
}
.searchable(text: $searchText)
}
}
}
struct NestedListView: View {
var items: [String]
#State var searchText = ""
var body: some View {
List {
ForEach(items, id: \.self) { item in
Text(item)
}
}
.searchable(text: $searchText)
}
}
I'm trying the iOS 16 beta, MacOS 13 beta, and Xcode 14 beta. I can't get TextField to edit a property within a #Binding struct. It works with a #State struct variable, but not binding. Anyone else seeing this problem?
I filed a Feedback with Apple a couple of days ago, but no response yet. I imagine they are swamped the week after WWDC.
MacOS Version 13.0 Beta (22A5266r)
Xcode Version 14.0 beta (14A5228q)
Here's a code example of the problem.
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationStack {
List {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
}
/// Click on Category List
NavigationLink {
CatList()
} label: {
Text("Category List")
}
}
}
}
}
struct CatList: View {
/// Click on bar
#State var catArray: [Categories] = [ Categories(id: UUID(), myName: "foo"), Categories(id: UUID(), myName: "bar")]
var body: some View {
List {
ForEach($catArray) { $eachCat in
NavigationLink {
CatDetails(thisCategory: $eachCat)
} label: {
Text(eachCat.myName)
}
}
}
}
}
struct CatDetails: View {
#Binding var thisCategory: Categories
var body: some View {
Form {
/// Cannot edit bar.
TextField("Name:", text: $thisCategory.myName)
}
}
}
struct Categories: Identifiable, Hashable {
var id: UUID
var myName: String
}
Currently I've a picker included in a Section included in a Form what I'm trying to reach is to align the selected value of the picker to the leading in both iOS 13 and 14, I've tried many solutions such as labelsHidden() but with no result, kindly find the code sample that generates the following screenshot on iOS 14, any help would be appreciated
struct ContentView: View {
#State private var selectedStrength = "Mild"
let strengths = ["Mild", "Medium", "Mature"]
var body: some View {
NavigationView {
Form {
Section {
Picker("", selection: $selectedStrength) {
ForEach(strengths, id: \.self) {
Text($0)
}
}
}
}
}
}
}
Use the Text() with a Spacer() in a HStack()
struct ContentView: View {
#State private var selectedStrength = "Mild"
let strengths = ["Mild", "Medium", "Mature"]
var body: some View {
NavigationView {
Form {
Section {
Picker("", selection: $selectedStrength) {
ForEach(strengths, id: \.self) { t in
HStack {
Text(t)
Spacer()
}
}
}
}
}
}
}
}
You have to use .frame() and .labelsHidden()
struct ContentView: View {
#State private var selectedStrength = "Mild"
let strengths = ["Mild", "Medium", "Mature"]
var body: some View {
NavigationView {
Form {
Section {
Picker("", selection: $selectedStrength) {
ForEach(strengths, id: \.self) {
Text($0)
}
}
.frame(width: 160, alignment: .leading)
.labelsHidden()
}
}
}
}
}
tested on IOS 16
SwiftUI’s Tab selection is suppose to work with any hashable content however that doesn’t seem to work.
In the example provided, you can see that in “Working” Tab, eveything works correctly if you use an integer for the tab selection. When you switch over to the “Broken” tab, the selection is a ColorItem and the selection does not update the view.
I believe this is a SwiftUI bug and have filed a feedback(FB8879981).
Tested with Xcode 12.2 and iOS 14.2(RC).
struct ColorItem: Identifiable, Hashable{
let color: Color
let title: String
var id: String{
title
}
}
struct ContentView: View {
let items = [
ColorItem(color: .red, title: "Red"),
ColorItem(color: .blue, title: "Blue"),
ColorItem(color: .purple, title: "Purple")
]
var body: some View {
TabView{
TabViewWorking(items: items)
.tabItem {
Label("Working", systemImage: "hand.thumbsup")
}
TabViewBroken(items: items)
.tabItem {
Label("Broken", systemImage: "hand.thumbsdown")
}
}
}
}
struct TabViewWorking: View {
#State private var tabSelection = 0
let items: [ColorItem]
var body: some View {
ZStack{
TabView(selection: $tabSelection){
ForEach(0..<items.count){ i in
items[i].color.edgesIgnoringSafeArea(.all)
.tag(i)
}
}
.tabViewStyle(PageTabViewStyle())
VStack{
Text(tabSelection.description)
Text(items[tabSelection].title)
}
.font(.largeTitle)
}
}
}
struct TabViewBroken: View {
#State private var tabSelection = ColorItem(color: .red, title: "Red")
let items: [ColorItem]
var body: some View {
ZStack{
TabView(selection: $tabSelection){
ForEach(0..<items.count){ i in
items[i].color.edgesIgnoringSafeArea(.all)
.tag(i)
}
}
.tabViewStyle(PageTabViewStyle())
VStack{
Text(items.firstIndex(of: tabSelection)?.description ?? "N/A")
Text(tabSelection.title)
}
.font(.largeTitle)
}
}
}
No, it is not SwiftUI bug. Type of selection and type of tag must be same, so in your first scenario they are both integers, but in second one they are not same - selection is ColorItem, but tag is still integer - thus selection does not work.
Here is fixed variant:
struct TabViewBroken: View {
#State private var tabSelection = ColorItem(color: .red, title: "Red")
let items: [ColorItem]
var body: some View {
ZStack{
TabView(selection: $tabSelection){
ForEach(0..<items.count){ i in
items[i].color.edgesIgnoringSafeArea(.all)
.tag(items[i]) // << here !!
}
}
.tabViewStyle(PageTabViewStyle())
VStack{
Text(items.firstIndex(of: tabSelection)?.description ?? "N/A")
Text(tabSelection.title)
}
.font(.largeTitle)
}
}
}
After upgrade to Xcode 11 beta 6, DefaultPicker is behaving like a wheelPicker
var colors = ["Mumbai", "Delhi", "Chennai", "Hyderabad"]
#State private var selectedColor = 0
var body: some View {
VStack {
Picker(selection: $selectedColor, label: Text("Please choose a city")) {
ForEach(0 ..< colors.count) {
Text(self.colors[$0])
}.pickerStyle(DefaultPickerStyle())
}
}
}
}
I wanted single list row that navigates into a new list of possible optionsPicker
I found the issue added the picker inside Form, here the final code
#State private var selectedColor = 0
var body: some View {
NavigationView {
Form {
Picker(selection: $selectedColor, label: Text("Please choose a city")) {
ForEach(0 ..< colors.count) {
Text(self.colors[$0])
}.pickerStyle(DefaultPickerStyle())
}
}
}
}
I don't know what you are exactly trying to achieve but I guess you want to use segement picker.
replace
DefaultPickerStyle()
with
SegmentedPickerStyle()