Is there a way to disable the SplitView using SwiftUI on iPad inside a navigation view?
By setting the NavigationViewStyle
import SwiftUI
struct NavView: View {
var body: some View {
NavigationView{
List{
NavigationLink(destination: TestView(), label: {Text("TestView")})
}
}.navigationViewStyle(StackNavigationViewStyle())
}
}
struct NavView_Previews: PreviewProvider {
static var previews: some View {
NavView()
}
}
Related
I'm trying to change my status bar color dynamically without success, anyone has any idea how to change it ?
I'm not using UIHosting controller so there is no AppDelegate or SceneDelegate fully using swiftUI:
#main
struct MyProjectApp: App{}
If i set ZStack {}.preferredColorScheme(.light) it only apply once, so if i go to another view and try to put ZStack {}.preferredColorScheme(.dark) it doesn't work.
I think i have try all the question here on stackoverflow, so if anyone have a definitive solution i appreciated.
#Updated code example
struct ContentA: View {
var body: some View {
NavigationView {
ZStack {
NavigationLink(destination: ContentB()) {
Text("Show Detail View")
}.navigationBarTitle("Navigation")
}.preferredColorScheme(.light)
}
}
}
struct ContentB: View {
var body: some View {
ZStack {
Text("Hello ContetB")
.padding()
.foregroundColor(.green)
}.preferredColorScheme(.dark)
}
}
If you try to change StatusBar color using this way it doesn't work.
The colors within the app should be dynamic automatically, unless you set them specifically. You can toggle the .preferredColorScheme within the Previews section.
struct ContentView: View {
var body: some View {
Text("Hello, world!")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.preferredColorScheme(.dark)
}
}
I want to open a view after taping on a push notification. The views and notifications are all working fine but my problem is when I try to open the view from the push notification via .sheet(isPresented) it is always presenting the view as modal.
I would like to achieve that the view is actually opened embedded in the NavigationView it would usually be when accessing it manually.
To make it more clear:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView{
VStack{
NavigationLink(destination: ListView()){
Text("Going one level down")
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct ListView: View {
var body: some View{
NavigationLink(destination: DetailView()){
Text("Click me")
}
}
}
struct DetailView: View {
var body: some View{
Text("I want to display this view after tapping on a notification.")
}
}
After tapping on the push notification, I want to jump directly to the DetailView() down in the NavigationView tree (similar to what the Apple Calendar app is doing when you tap on the notification).
Any ideas?
In my app when I navigate to another tab and scroll down the view and gets cut off and the navigation bar doesn't collapse as shown here:
I tried to put .edgesIgnoringSafeArea(.top) but then this happens:
When I press the home button the simulator and navigate back to the app the navigation bar will collapse when scrolling as intended. Is this a bug with Xcode? Currently I'm using XCode 11.4.1, testing on an iPhone 11 Pro Max simulator but the exact same result occurs on my physical iPhone 6s Plus.
EDIT: The code for the tab view is as follows:
import SwiftUI
struct MenuScreen: View {
#State private var selection = 0
var body: some View {
TabView(selection: $selection){
ItemsTab().tabItem{
Image(systemName: "phone.fill")
Text("Items")
}.tag(0)
TestTab().tabItem{
Image(systemName: "phone.fill")
Text("Test")
}.tag(1)
}
.navigationBarTitle("Menu")
// .edgesIgnoringSafeArea(.top)
.navigationBarItems(trailing: NavigationLink(destination:ProfileScreen()){Text("Profile")})
// .padding(.top,1)
.navigationViewStyle(DefaultNavigationViewStyle())
// .navigationBarHidden(true)
}
}
struct MenuScreen_Previews: PreviewProvider {
static var previews: some View {
MenuScreen()
}
}
The navigation view is wrapped inside a splash screen like so:
import SwiftUI
struct SplashScreen: View {
#State private var isActive = false
let content = ContentView()
var body: some View {
NavigationView{
VStack{
Text("Loading")
LoopingAnimation()
NavigationLink(destination: content,isActive: $isActive,label: {EmptyView()})
}.onAppear(perform: {
self.goToContentView(time:2.5)
}).navigationBarTitle("My app")
}
}
func goToContentView(time:Double){
DispatchQueue.main.asyncAfter(deadline: .now() + Double(time)){
self.isActive = true
}
}
}
struct SplashScreen_Previews: PreviewProvider {
static var previews: some View {
SplashScreen()
}
}
EDIT 2: I tried to put navigation views inside the tabview as shown here:
import SwiftUI
struct MenuScreen: View {
var body: some View {
TabView{
NavigationView{
ItemsTab().navigationBarTitle("Items")
}.tabItem{
Image(systemName: "house.fill")
Text("Items")
}
NavigationView{
TestTab().navigationBarTitle("Test")
}.tabItem{
Image(systemName: "phone.fill")
Text("Test")
}
}
.navigationBarHidden(true)
// .navigationBarBackButtonHidden(true)
.frame(alignment: .center)
// .edgesIgnoringSafeArea(.top)
.navigationBarItems(trailing: NavigationLink(destination:ProfileScreen()){Text("Profile")})
// .padding(.top)
.navigationViewStyle(DefaultNavigationViewStyle())
}
}
struct MenuScreen_Previews: PreviewProvider {
static var previews: some View {
MenuScreen()
}
}
But while it resulted in a collapsing navigation bar that worked even switching tabs the result looked like this:
Made the navigation bar stay collapsed in the tab screen similar to what happens when you navigate to other categories within a tab in the app store by setting .displayMode inside .navigationBarTitle to .inline
Screenshot:
I needed to add a view to my scene using a view transition. I was trying to use the modifier .transition(.opacity) but it just adds the view without any transition. I also tried the other transition types and they don't work. Since this is a really simple test project a don't see where's the issue. Here's a link to a video of my problem
import SwiftUI
struct Test: View {
#State var show = false
var body: some View {
VStack {
Button(action: {self.show.toggle()}) {
Text("Button")
}
if show {
Text("Test Text")
.transition(.move(edge: .top))
}
}
}
}
struct Test_Previews: PreviewProvider {
static var previews: some View {
Test()
}
}
You need to add animation for the transition
if show {
Text("Test Text").animation(.linear)
.transition(.move(edge: .top))
}
I am writing an app using SwiftUI, and I am having some issues with the NavigationView and the Navigation Link, as shown below in the simulator video. The project was created as a Single View App.
I have three pages, and as you can see when it takes me from one page to the other, it builds the second page under the first one, and doesn’t dellocate the first one, and I have the same problem on the third page: it is created below the second one, and I can even go back to the first page via the faulty back button on the top left.
Video:
This is the code of the three pages:
first.swift
import SwiftUI
struct first: View {
var body: some View {
NavigationView{
NavigationLink(destination: second()) {
Text("first link")
}
}
}
}
struct first_Previews: PreviewProvider {
static var previews: some View {
first()
}
}
second.swift
import SwiftUI
struct second: View {
var body: some View {
NavigationView {
NavigationLink(destination: third()) {
Text("second link")
}
}
}
}
struct second_Previews: PreviewProvider {
static var previews: some View {
second()
}
}
third.swift
import SwiftUI
struct third: View {
var body: some View {
Text("Third page")
}
}
struct third_Previews: PreviewProvider {
static var previews: some View {
third()
}
}
I am using the Navigation View in the same manner that the official apple tutorial does (or at least I think I am…) here
Thanks!
Edit: I'm still having the same problem with this code, despite having defined only one NavigationView. The problem came back after I uploaded to Xcode 11.2, but in the Release Notes it doesn't say anything about any changes made to the NavigationView.
first.swift
import SwiftUI
struct first: View {
var body: some View {
NavigationView {
NavigationLink(destination: second()) {
Text("First link")
}
}
}
}
struct first_Previews: PreviewProvider {
static var previews: some View {
first()
}
}
second.swift
import SwiftUI
struct second: View {
var body: some View {
NavigationLink(destination: first()) {
Text("back to first")
}
}
}
struct second_Previews: PreviewProvider {
static var previews: some View {
second()
}
}
Video:
Thanks again!
What your current code is doing is nesting the NavigationView in second inside of the one in first.
To get around this you just need to delete the NavigationView in second:
struct second: View {
var body: some View {
NavigationLink(destination: third()) {
Text("second link")
}
}
}