Lag when is used clipShape and Circle on SwiftUI - swiftui

I am facing a problem with SwiftUI's clipShape().
When I use clipShape() with Circle(), my iPad (iPad air 2) is lagging + black screen but with Capsule I haven't this lag.
This GIF presents you this problem :
clipShape with circle
And this GIF presents you clipShape() with Capsule() where I don't see any problem :
clipShape with capsule
This is the code I used to test this:
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
ForEach(1 ..< 5) { i in
HStack {
ForEach(1 ..< 15) { t in
Image(systemName: "plus")
.padding(10)
.background(Color.gray)
.clipShape(Circle())
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Thank you very much.

Related

How to add larger navigation title for macOS like in iOS

htpA here!
I am a newbie of SwiftUI programming and I am just trying to add a navigation title for macOS like in iOS. I tried many things but it didn't seem to be working. I hope anyone would answer it quickly.
Here's my code
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List {
Spacer()
NavigationLink(destination: LatestEpisodes()) {
Menu1()
}
.frame(minWidth: 110)
.frame(maxWidth: 110)
}
Text("Content")
}
.navigationTitle("Hello!")
.frame(minWidth: 900)
.listStyle(SidebarListStyle())
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct Menu1: View {
var body: some View {
VStack {
Image(systemName: "1.circle.fill")
Text("Menu1")
}
.frame(width: 90, height: 90)
.padding(10)
}
}

Adding navigation bar to smaller view in SwiftUI

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

Placing TabView inside NavigationView makes header transparent

TabView worked fine for me before i added NavigationView around it.
Here's a normal behaviour
A problem arises when i add NavigationView around my TabView - when scrolling I see "test" in front of black rectangle.
I tried setting
UINavigationBar.appearance().backgroundColor
to some opaque color but it just draws another rectangle in front of everything.
Is there any way to make that black rectangle opaque when using NavigationView around TabView?
Oh, one more thing.. i tried removing
.edgesIgnoringSafeArea(.top)
which solves the problem but huge white space appears in top area:
import SwiftUI
struct ContentView: View {
let tabData = [
TabItem(menuTitle: Text("item 1"), menuImage: Image(systemName: "heart.fill"), bodyHeadline: Text("Postnatal recovery"), bodyMeta: Text(""), tag: 1),
TabItem(menuTitle: Text("item 2"), menuImage: Image(systemName: "heart.fill"), bodyHeadline: Text("Weight loss"), bodyMeta: Text(""), tag: 2),
]
var body: some View {
NavigationView {
ZStack {
TabView {
ForEach(tabData) { tabItem in
VStack {
VStack {
Rectangle()
}
.frame(height: 90)
.background(Color.yellow)
ScrollView {
LazyVStack(spacing: 0) {
ForEach(0...100, id:\.self) { val in
ZStack {
Text("test")
.font(.system(size: 128))
} // ZStack
.background(Color.white)
} // ForEach
}
}
.background(Color.blue)
}
.tabItem {
tabItem.menuImage
tabItem.menuTitle
.foregroundColor(Color.red)
}
.edgesIgnoringSafeArea(.top)
} // ForEach
} // TabView
} // ZStack
} // NavigationView
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct TabItem: Identifiable {
var id = UUID()
var menuTitle: Text
var menuImage: Image
var bodyHeadline: Text
var bodyMeta: Text
var tag: Int
}
adding
.navigationBarHidden(true).navigationBarTitle("")
made everything work smoothly

SwiftUI: animating between Single and HStack views

Goal:
1- Shows a view (Blue) that covers entire screen
2- When tapped on bottom (top right corner), it shows an HStack animating right side HStack (Green) "Slide Offset animation".
import SwiftUI
struct ContentView: View {
#State var showgreen = false
var body: some View {
NavigationView {
HStack {
Rectangle()
.foregroundColor(.blue)
if showgreen {
Rectangle()
.foregroundColor(.green)
.offset(x: showgreen ? 0 : UIScreen.main.bounds.width)
.animation(.easeInOut)
}
}
.navigationBarItems(trailing:
Button(action: { self.showgreen.toggle() }) {
Image(systemName: "ellipsis")
})
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.colorScheme(.dark)
.previewDevice("iPad Pro (12.9-inch) (3rd generation)")
}
}
The code works, however I cannot get the Green "Slide Offset animation" to work. Really appreciate any help! : )
Instead of using the if conditional, you need the green rectangle to be already present, and just offscreen. When showgreen toggles, you need to shrink the size of the blue rectangle, which will make room for the green rectangle.
struct ContentView: View {
#State var showgreen = false
var body: some View {
NavigationView {
HStack {
Rectangle()
.foregroundColor(.blue)
.frame(width: showgreen ? UIScreen.main.bounds.width / 2 : UIScreen.main.bounds.width)
.animation(.easeInOut)
Rectangle()
.foregroundColor(.green)
.animation(.easeInOut)
}
.navigationBarItems(trailing:
Button(action: { self.showgreen.toggle() }) {
Image(systemName: "ellipsis")
})
}
.navigationViewStyle(StackNavigationViewStyle())
}
}

SwiftUI padding horizontally gives unexpected gap

I have the following view:
import SwiftUI
struct ContentView: View {
var body: some View {
ZStack{
Color.green.edgesIgnoringSafeArea(.all)
VStack {
HStack{
Text("header leading")
Spacer()
Text("header trailing")
}
// this makes the unexpected gap somehow
.padding(.horizontal)
.frame(height: 60)
.background(Color.red)
VStack{
Text("body top")
Spacer()
Text("body bottom")
}
.background(Color.blue)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Why do I get this gap between the red HStack and the blue VStack? If I remove padding, the gap is gone.
For my understanding, the padding should only make in indent horizontally (leading and trailing). Why is this assumption wrong?
import SwiftUI
struct ContentView: View {
var body: some View {
ZStack{
Color.green.edgesIgnoringSafeArea(.all)
VStack(spacing:0.0) {
HStack{
Text("header leading")
Spacer()
Text("header trailing")
}
// this makes the unexpected gap somehow
.padding(.horizontal)
.frame(height: 60)
.background(Color.red)
VStack{
Text("body top")
Spacer()
Text("body bottom")
}
.background(Color.blue)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
[screen capture in simulator iphone 8][1]