Sorry for the cryptic title.
I would like to have part of a row inside a list of rows to to animate downwards without moving the views vertically above it.
Here is a minimal example, showing what I mean:
import SwiftUI
struct ContentView: View {
#State var display = false
var body: some View {
List {
Button("Toggle") {
display.toggle()
}
Text("Text")
row
Text("Text")
}
.listStyle(.plain)
.animation(.linear, value: display)
}
var row: some View {
VStack {
Text("Title").bold()
if display {
Text("Lorem")
Text("ipsum")
Text("dolor")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.previewDevice(.init(rawValue: "iPhone 13 mini"))
}
}
The lorem ipsum block is supposed to animate into view without moving the Title text.
I imagine there's no way around GeometryReader here, but wanted to make sure. All suggestions are much appreciated 🙏
Related
Hi I am currently working on swiftUI project. I am facing a very strange issue. Here is the code for my problem. As you can see that, this tabView is taking some space from the Top which is highlighted in Red Color. It should be All blue in the View.
Why it is taking that extra space from the Top?
Kindly Review the code and help me with this problem as i'm a beginner.
import SwiftUI
struct StackOverFlowView: View {
#State private var selection = 0
var body: some View {
ZStack() {
Color.red
TabView(selection: $selection) {
Color.blue
}
.tabViewStyle(PageTabViewStyle())
.edgesIgnoringSafeArea(.all)
}.ignoresSafeArea(.all)
}
}
struct StackPreview: PreviewProvider {
static var previews: some View {
StackOverFlowView()
}
}
I need some suggestions on presenting a segmented picker in Swift UI.
It is to display distinct time ranges (<15min, <30min, <45min) all the way to 120min.
It ends up being 8 segments. I am really not a fan of the scrolling picker as it not in theme what what I am looking for in presentation.
The problem with how it stands now is that the time unit is cut off with each segment showing "15.." and doesn't look clean.
I have put the segmented picker in a horizontal scroll view which looks okay but the user may not know to scroll.
One option I used but can't get to work out is splitting the one long segment into 2 separate views.
The problem is the user can select a segment from either pickers which is not what I want.
What I want is if the user selects one picker, the other one is not selectable or vice versa.
I have been messing with some formatting options, so please ignore that.
Is this possible?
Thanks is advance!
struct ContentView: View {
var body: some View {
VStack{
To60min()
To120min()
.foregroundColor(Color.red)
}
}}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}}
struct To60min: View {
#State private var selectedTimeRangeto60 = ""
#State private var timeRangesTo60 = ["15min", "30min", "45min", "60min"]
var body: some View {
Picker("", selection: $selectedTimeRangeto60) {
ForEach(timeRangesTo60, id: \.self) {
Text($0)
}
}
.frame(width: .infinity, height: 75)
.background(.gray)
.padding()
.pickerStyle(.segmented)
.contrast(22.0)
}
}
struct To120min: View {
#State private var selectedTimeRangeto120 = ""
#State private var timeRangesTo120 = ["75min", "90min", "105min", "120min"]
var body: some View {
Picker("", selection: $selectedTimeRangeto120) {
ForEach(timeRangesTo120, id: \.self) {
Text($0)
}
}
.padding()
.pickerStyle(.segmented)
.contrast(22)
}
}
For anything more than 3-4 items (depending on label length), I would switch from a .segmented to .menu picker style. https://developer.apple.com/documentation/swiftui/pickerstyle
I followed this tuto : https://www.youtube.com/watch?v=mUMR5TCqpDU
Until the end it's ok. But now I would like to add a button.
So I created : bouton1 and 2 at the top
And this is the header of my page : headerview
I would like to go on another view when I click on Bouton2 for example. I guess it's a basic functionality, BUT I when I add navigationview that change everything on my page..
I guess it's because I have a header that should be different..
I tried with
Here my HeaderView.swift
import SwiftUI
struct HeaderView: View {
var body: some View {
NavigationView {
Text("ttt").navigationBarItems(leading:
NavigationLink(destination: GarageView()) {
Text("ey")
},
trailing:
Button(action: {}) {
Text("ee")
}
)
}
.accentColor(.green)
}
}
struct HeaderView_Previews: PreviewProvider {
static var previews: some View {
HeaderView()
.previewLayout(.fixed(width: 375, height: 300)) //375 80
}
}
and my contentView.swift :
import SwiftUI
struct ContentView: View {
var body: some View {
VStack{
// Top Stack
HeaderView()
// Card
ZStack {
ForEach(Card.data.reversed()) { card in
CardView(card: card).padding(10)
}
}.zIndex(1.0)
// Bottom Stack
FooterView()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
but the result is : I don't know why the header is big like this then i click on the button "ey" -> as you can see the new page is the half of my screen
I would like my new page take the entire screen...
Do you have an idea ?
Thank you.
This question already has an answer here:
SwiftUI iOS14 - NavigationView + List - Won't fill space
(1 answer)
Closed 2 years ago.
If I wrap a VStack with a List inside a NavigationView, the resulting list is indented, see image. This is not the expected formatting because I'd want the list to cover the whole screen width.
How can I fix this and why does this happen?
Note: This indentation behavior would go away if I remove the VStack, but here I do want to include the Text box ("Something...") inside the NavigationView, because I want it to go away once a user clicks on an item.
Full code below, Xcode 12.3:
import SwiftUI
struct ListView: View {
var items: [String]
var body :some View {
List {
ForEach(items, id: \.self) { item in
NavigationLink(destination: Text(item)){
Text(item)
}
}
}
}
}
struct ContentView: View {
var body: some View {
NavigationView{
VStack{
Text("Something that should disappear when I click on the item")
ListView(items: ["a", "b"])
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Hey all you need to do is add the modifier for a .liststyle
.listStyle(InsetListStyle())
Also you should be aware that there is no way to remove the chevron symbol on lists
import SwiftUI
struct ListView: View {
var items: [String]
var body :some View {
List {
ForEach(items, id: \.self) { item in
NavigationLink(destination: Text(item)){
Text(item)
}
}
}.listStyle(InsetListStyle())
}
}
struct ContentView: View {
var body: some View {
NavigationView{
VStack{
Text("Something that should disappear when I click on the item")
ListView(items: ["a", "b"])
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I am new in swiftUI, and I know how to do it in Xcode to make two different random "a" and "b",and print the output. However, I do not know how in SwiftUI to show two different "a" and "b".
It seems like if I use var again, it will deny it.
import SwiftUI
struct ContentView: View {
var a=Int.random(in:80...90)
var b=Int.random(in:60...70)
var body: some View {
VStack {
Text ("\(a)+\(b)=")
Text ("\(a)+\(b)=")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
You could do something like this:
struct ContentView: View {
var body: some View {
VStack {
Text ("\(Int.random(in:80...90))+\(Int.random(in:80...90))=")
Text ("\(Int.random(in:80...90))+\(Int.random(in:80...90))=")
}
}
}
If you use the same var a and var b for both text fields the same numbers will always appear regardless of how many times you run Int.random