I'm a newbie in SwiftUI and I have a little question.
I have made an Code for a Menu Bar, but when I run the Preview the "Gradient" isn't show.
Everything is white in preview.
code
preview
I hope u can help me guys!
Thanks
Change
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
...
}
}
to
struct Menu_Previews: PreviewProvider {
static var previews: some View {
...
}
}
LinearGradient(gradient: Gradient(colors: [Color.blue, Color.red]), startPoint: .top, endPoint: .bottom)
In the LinearGradient, you have to say Color.blue and Color.red instead of Color("blue"), Color("red").
Otherwise, you are good to go.
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()
}
}
Curious how the following navigation bar can be achieved using SwiftUI, does a UI that mimics the looks of navigation bar need to made or is there a way to add an actual NavigationView?
I think yes? Consider the following code.
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("Hello, world!")
.padding()
Spacer()
miniNavigationView
Spacer()
Text("Hello, world!")
.padding()
}
.background(.red)
}
var miniNavigationView: some View {
HStack {
NavigationView {
ScrollView {
VStack {
Text("Inside the mini Navigation View!")
}
}
.navigationTitle("Inside the mini Navigation view!")
}
}
.frame(width: 250, height: 250)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Gif of Result
import SwiftUI
struct FolderView: View {
var body: some View {
NavigationView{
VStack{
HStack{
Text("hi")
}
.frame(height : 50)
.frame(maxWidth : .infinity)
.background(.blue)
List {
Text("hi")
}
}
.navigationTitle("Task Folders 📁")
}
}
}
struct FolderView_Previews: PreviewProvider {
static var previews: some View {
FolderView()
}
}
Hi! I tried to use Hstack container in NavigationView with List, But as you can see in my attached screenshot, it is working like that, I mean the container is mixed with navigationView Area.
Is there a some way that I can solve? How I can use that with List?
Thank you!
Here is fixed variant - use as a background not a color but filled rectangle. Tested with Xcode 13.2 / iOS 15.2
HStack{
Text("hi")
}
.frame(height : 50)
.frame(maxWidth : .infinity)
.background(Rectangle().fill(Color.blue)) // << here !!
Note: it is not clear for now whether that is a bug or a new NavigationView+background(color) feature.
I am trying to implement a simple "tap to toggle the visibility of the UI" in SwiftUI with fade in/out animation. The following code animates the fade-in effect of the Text element as I expected, but it immediately hides the Text element when isVisible become false.
I'd like to understand why this code does not work, and how to fix it in the most natural way.
import SwiftUI
struct ContentView: View {
#State var isVisible = true
var body: some View {
ZStack {
Rectangle()
.foregroundColor(.blue)
.gesture(TapGesture(count: 1).onEnded {
withAnimation(.easeInOut(duration: 1.0)) {
isVisible.toggle()
}
})
if isVisible {
Text("Tap me!")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I'm using Xcode 12.5 on Big Sur, and my iPhone is running iOS 14.5.1.
Thanks to Erik Philips, here is the answer.
import SwiftUI
struct ContentView: View {
#State var isVisible = true
var body: some View {
ZStack {
Rectangle()
.zIndex(1)
.foregroundColor(.blue)
.gesture(TapGesture(count: 1).onEnded {
withAnimation(.easeInOut(duration: 1.0)) {
isVisible.toggle()
}
})
if isVisible {
Text("Tap me!")
.zIndex(2)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
See attached:
Squished Toggle in SwiftUI
... The code for this centered toggle is simply:
#State var rememberMe: Bool = false
HStack(alignment: .center) {
Spacer()
Toggle(
"Remember Me",
isOn: $rememberMe
)
Spacer()
}
Obviously, there's no need for ellipsis here, but by eliminating the Spacer() on each side, it'll grow to fill the width.
How do you center the Toggle without squishing its text? 🤔
fixedSize() seems to fix the issue on my end. We don't even need a HStack:
import SwiftUI
struct ContentView: View {
#State var rememberMe: Bool = false
var body: some View {
Toggle(
"Remember Me",
isOn: $rememberMe
).fixedSize()
}
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
Result
From Apple:
fixedSize() Fixes this view at its ideal size.