Running PageTabViewStyle in landscape orientation on any iPhone with a notch results in odd spacing on the leading side of the view, edgesIgnoringSafeArea does not seem to be working correctly? Adding a second Edges ignore in the body seems to help, but still left with a leading white area. PageTabViewStyle and edgesIgnore seem to work fine on older non-notch phones. Anyone know a solution to this problem?
import SwiftUI
struct TestTabView: View {
var colors : [Color] = [.red, .yellow, .blue, .green]
@State private var page: Int = 1
var body: some View {
TabView (selection: $page){
ForEach(0..<colors.count, id: \.self) {index in
Rectangle().foregroundColor(colors[index]).edgesIgnoringSafeArea(.all)
.tag([index])
}
}
.tabViewStyle(PageTabViewStyle())
.edgesIgnoringSafeArea(.all)
}
}
struct TestTabView_Previews: PreviewProvider {
static var previews: some View {
TestTabView()
}
}
Post
Replies
Boosts
Views
Activity
Creating a new Core data Entry/Object (addItem) while in modal stops the dismissal of the view, presentationMode.wrappedValue.dismiss() no longer works. The sample below uses the template provided in Beta 6 with core data, with the additional of a full screen cover to save some data. After the new Item is created, the modal will no longer dismiss. Any way around this?
import SwiftUI
import CoreData
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],animation: .default) private var items: FetchedResults<Item>
@State private var isPresented = false
var body: some View {
NavigationView {
List {
ForEach(items) { item in
Text("\(item.timestamp!, formatter: itemFormatter)")
}
.onDelete(perform: deleteItems)
}
.toolbar {
Button(action: {isPresented.toggle()}) {Image(systemName: "plus")}
.fullScreenCover(isPresented: $isPresented, content: { PopUpView() })
}
}
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
offsets.map { items[$0] }.forEach(viewContext.delete)
do {
try viewContext.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
}
private let itemFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .medium
return formatter
}()
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}
struct PopUpView: View {
@Environment(\.managedObjectContext) private var viewContext
@Environment(\.presentationMode) private var presentationMode
var body: some View {
Button(action: addItem) {Text ("Add Item")}
Button(action: {presentationMode.wrappedValue.dismiss()} ) {Text ("Dismiss View")}
}
private func addItem() {
let newItem = Item(context: viewContext)
newItem.timestamp = Date()
do {
try viewContext.save()
self.presentationMode.wrappedValue.dismiss()
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
Adding a Shadow to a ScrollView View with a Transition seems to result in an offset issue when using an onTapGesture. The sample code below has a transitioned View with a ScrollView and onTapGesture. Adding a shadow causes an offset, so the onTabGesture picks the wrong image. Once you begin scrolling, the shadowed scrollview works as intended. Removing the Shadow from the ScrollView fixes the offset issue. Leaving the Shadow and removing the Transition also fixes the issue. I can seem to find a solution which would allow both Shadow and Transition?import SwiftUI
struct ContentView: View {
@State var transition : Bool = false
@State var backgroundImage : Image
var body: some View {
VStack {
Button( action: {withAnimation{self.transition.toggle()}})
{Text("ScrollView").foregroundColor(.black)}
.padding(20)
.background(Color.blue)
.padding(.bottom, 10)
Spacer()
if transition {pictureView1(image: $backgroundImage).transition(.slide)}
Spacer()
}
.background(backgroundImage)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(backgroundImage: Image(uiImage: UIImage() ))
}
}
//with Shadow, tapGesture picks wrong image, when shadow is removed, tapGesture picks correct image
struct pictureView1: View {
var pictures : [String] = ["dog1","dog2","dog3","dog4","dog5"]
@Binding var image : Image
var body: some View {
ScrollView(.horizontal) {
HStack {
ForEach(pictures, id: \.self) {picture in
Image(picture).resizable().frame(width: 150, height: 200, alignment: .center)
.onTapGesture {
self.image = Image(picture)
}
}
}
}
.frame(width: 350, height: 220)
.background(Color.gray)
.shadow(radius: 5, x: 5, y: 5) //with shadow removed, tapGesture works correctly, no scroll offset issue
}
}SceneDelegate:func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView(backgroundImage: Image(uiImage: UIImage()) )