I'm using SwiftUI with Version 11.3.1 (11C504).
I Implement Tab bar on the Screen and that screen will come after the navigation from login Screen.
If navigation displayMode is .automatic so i’am showing a white space(which is in bordered with black line in picture) when I scroll a list in up side. Otherwise navigation title show.
And When I set displayMode, .inline so it works perfect. And when I run a project so many time so some time it works perfect and some time showing a space.
i mentioned a white space in the picture with black border.
//ContentView
struct ContentView: View {
var body: some View {
HomeTabView()
}
}
//HomeTabView
struct HomeTabView: View {
#State private var selection = 0
//Inclose user intraface of tab View.
var body: some View {
TabView(selection: $selection){
TestListView().tabItem {
Image(systemName: "book.fill")
Text("Learn")
}.tag(0)
Text("Community").tabItem {
Image(systemName: "globe")
Text("Community")
}.tag(1)
//Add Notification List on the Screen.
Text("Notification").tabItem {
Image(systemName: "bell.fill")
Text("Notification")
}.tag(3)
//Add Account on the Tab Bar
Text("Account").tabItem {
Image(systemName: "person.circle.fill")
Text("Account")
}.tag(4)
}.accentColor(.pink)
.navigationBarTitle("SwiftUI")
}
}
TestListView
struct TestListView: View {
var body: some View {
VStack{
List(1...10, id: \.self){ num in
ListCards()
}
}.edgesIgnoringSafeArea(.all)
}
}
//ListCards
struct ListCards: View {
var body: some View {
ZStack{
RoundedRectangle(cornerRadius: 16)
.frame(height: 180)
.foregroundColor(.white)
.shadow(radius: 5)
VStack(alignment: .leading, spacing: 10){
HStack(alignment: .top){
Rectangle()
.frame(width: 100, height: 100)
.cornerRadius(16)
.foregroundColor(.pink)
VStack(alignment: .leading, spacing: 4){
Text("SwiftUI")
.font(.title)
Text("Description of title")
.foregroundColor(.gray)
}
}
.padding()
}.padding(.leading, 2)
}.padding(.all, 6)
}
}
You need to change your HomeTabView in this way:
var body: some View {
TabView(selection: $selection) {
NavigationView {
TestListView()
.navigationBarTitle("SWIFTUI")
}.tabItem {
Image(systemName: "book.fill")
Text("Learn")
}.tag(0)
NavigationView {
Text("Community")
}.tabItem {
Image(systemName: "globe")
Text("Community")
}.tag(1)
//Add Notification List on the Screen.
NavigationView {
Text("Notification")
}.tabItem {
Image(systemName: "bell.fill")
Text("Notification")
}.tag(3)
//Add Account on the Tab Bar
NavigationView {
Text("Account")
}.tabItem {
Image(systemName: "person.circle.fill")
Text("Account")
}.tag(4)
}.accentColor(.pink)
}
also, remove .edgesIgnoringSafeArea(.all) from TestListView
Related
I'm trying to get the entirety of a row to be double clickable.
Here is my code:
import SwiftUI
let colourArray = ["red","blue","green"]
struct ContentView: View
{
var body: some View
{
List
{
ForEach (colourArray, id: \.self)
{ colour in
ArrayRow(colour: colour)
.gesture(TapGesture(count: 2).onEnded
{
print("double clicked")
})
.padding(.bottom, 15)
}
}
}
}
struct ArrayRow: View
{
let colour: String
var body: some View
{
HStack(alignment: .top)
{
Text(colour)
.frame(width: 150, alignment: .leading)
Text(colour)
.frame(width: 150, alignment: .leading)
Text(colour)
.frame(width: 150, alignment: .leading)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
The double click works great, but only if you double click on the text. If you double click on the white space then nothing happens. How do I get the whole row to respond to the double click?
You can extend the tappable/clickable area with .contentShape
ArrayRow(colour: colour)
.contentShape(Rectangle())
.gesture(TapGesture(count: 2).onEnded
{
print("double clicked")
})
.padding(.bottom, 15)
I am completely new to SwiftUI so I hope this is not a stupid question. In my project I have a home view (a map) and a Menu button in the top right corner. Upon clicking that button my menu is gonna slide in from the left. For each of the menu items I want to use NavigationLink to jump to the next detail views.
Now here is the problem: When I click on the menu items (eg. Payments) I get the view but my button and my map are still on top of that view and I cant get rid of it, or at least I dont know how.. :(.
I saw a few similar questions but I couldnt make it work for my case.
This is my view when the menu view is true
This is my problem view. When I click on eg. Payments I cant get rid of the Map and the button in that view
This is my HomeView (The Map and the button)code:
import SwiftUI
struct HomeView: View {
#State var showMenu = false
var body: some View {
let drag = DragGesture()
.onEnded {
if $0.translation.width < -100 {
withAnimation {
self.showMenu = false
}
}
}
return GeometryReader { geometry in
ZStack(alignment: .leading) {
if self.showMenu {
SlideInMenuView()
.frame(width: geometry.size.width/1)
.transition(.move(edge: .leading))
}
HomeSupportView()
.frame(width: geometry.size.width, height: geometry.size.height)
.cornerRadius(20)
.scaleEffect(self.showMenu ? 0.8 : 1)
.offset(x: self.showMenu ? 150 : 0, y : self.showMenu ? 50 : 0)
.disabled(self.showMenu ? true : false)
MenuButton(showMenu: self.$showMenu)
}
.gesture(drag)
}
}
}
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
HomeView()
}
}
struct MenuButton: View {
#Binding var showMenu: Bool
var body: some View {
Button(action: {
withAnimation {
self.showMenu.toggle()
}
}){
Image(systemName: "line.horizontal.3")
.imageScale(.large)
.frame(width: 100, height: 60)
.background(Color.black)
.clipShape(Circle())
.opacity(0.8)
.foregroundColor(.white)
.rotationEffect(.degrees(self.showMenu ? 90 : 0))
.scaleEffect(self.showMenu ? 1.2 : 1)
}
.padding(.top, -380)
}
}
This is my menu view code with my try of making the NavigationLink work. I tried it on the "Payments" HStack in multiple versions but I just cant make it work
import SwiftUI
struct SlideInMenuView: View {
#State private var showingPaymentDetails = false
var body: some View {
NavigationView {
VStack(alignment: .leading) {
HStack {
Image(systemName: "person")
.foregroundColor(.gray)
.imageScale(.large)
Text("Profile")
.foregroundColor(.gray)
.font(.headline)
}
.padding(.top, 150.0)
HStack {
Image(systemName: "person.2.fill")
.foregroundColor(.gray)
.imageScale(.large)
Text("Matches")
.foregroundColor(.gray)
.font(.headline)
}
.padding(.top, 30)
NavigationLink(destination:
PaymentDetailsView())
{
HStack {
Image(systemName: "creditcard")
.foregroundColor(.gray)
.imageScale(.large)
Text("Payments")
.foregroundColor(.gray)
.font(.headline)
}
.padding(.top, 30)
}
HStack {
Image(systemName: "hammer")
.foregroundColor(.gray)
.imageScale(.large)
Text("Settings")
.foregroundColor(.gray)
.font(.headline)
}
.padding(.top, 30)
Spacer()
}
.padding()
.frame(maxWidth: .infinity, alignment: .leading)
.background(Color(red: 32/255, green: 32/255, blue: 32/255))
.edgesIgnoringSafeArea(.all)
}
}
}
struct SlideInMenuView_Previews: PreviewProvider {
static var previews: some View {
SlideInMenuView()
}
}
If anyone knows how to help me out here...
Thank you very much in advance guys.
Stay healthy.
Cheers,
LeBob
#Chris: This is how I want it to be. I put in 4 consecutive screenshots with the respective clicks.
***** UPDATED ANSWER *****
thank you for your clarifying pictures
try this:
import SwiftUI
class Global : ObservableObject {
#Published var showMenu = true
#Published var showMenuButton = true
}
struct PaymentDetailsView : View {
#EnvironmentObject var global : Global
var body: some View {
Text("payment")
.onAppear() {
withAnimation() {
self.global.showMenuButton = false
}
}
.onDisappear() {
withAnimation() {
self.global.showMenuButton = true
}
}
}
}
struct HomeSupportView : View {
#EnvironmentObject var global : Global
var body: some View {
ZStack {
Rectangle().fill(Color.blue)
Text("home")
}
}
}
struct SlideInMenuView: View {
#EnvironmentObject var global : Global
#State private var showingPaymentDetails = false
var body: some View {
NavigationView {
VStack(alignment: .leading) {
HStack {
Image(systemName: "person")
.foregroundColor(.gray)
.imageScale(.large)
Text("Profile")
.foregroundColor(.gray)
.font(.headline)
}
.padding(.top, 150.0)
HStack {
Image(systemName: "person.2.fill")
.foregroundColor(.gray)
.imageScale(.large)
Text("Matches")
.foregroundColor(.gray)
.font(.headline)
}
.padding(.top, 30)
NavigationLink(destination:
PaymentDetailsView().environmentObject(self.global))
{
HStack {
Image(systemName: "creditcard")
.foregroundColor(.gray)
.imageScale(.large)
Text("Payments")
.foregroundColor(.gray)
.font(.headline)
}
.padding(.top, 30)
}
HStack {
Image(systemName: "hammer")
.foregroundColor(.gray)
.imageScale(.large)
Text("Settings")
.foregroundColor(.gray)
.font(.headline)
}
.padding(.top, 30)
Spacer()
}
.padding()
.frame(maxWidth: .infinity, alignment: .leading)
.background(Color(red: 32/255, green: 32/255, blue: 32/255))
.edgesIgnoringSafeArea(.all)
}
}
}
struct SlideInMenuView_Previews: PreviewProvider {
static var previews: some View {
SlideInMenuView().environmentObject(Global())
}
}
struct ContentView: View {
#EnvironmentObject var global : Global
var body: some View {
let drag = DragGesture()
.onEnded {
if $0.translation.width < -100 {
withAnimation {
self.global.showMenu = false
}
}
}
return GeometryReader { geometry in
ZStack(alignment: .leading) {
if self.global.showMenu {
SlideInMenuView()
.environmentObject(self.global)
.frame(width: geometry.size.width/1)
.transition(.move(edge: .leading))
.zIndex(3)
}
HomeSupportView()
.environmentObject(self.global)
.frame(width: geometry.size.width, height: geometry.size.height)
.cornerRadius(20)
.scaleEffect(self.global.showMenu ? 0.8 : 1)
.offset(x: self.global.showMenu ? 150 : 0, y : self.global.showMenu ? 50 : 0)
.disabled(self.global.showMenu ? true : false)
.zIndex((2))
MenuButton().environmentObject(self.global)
.zIndex(4)
}
.gesture(drag)
}
}
}
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environmentObject(Global())
}
}
struct MenuButton: View {
#EnvironmentObject var global: Global
var body: some View {
Button(action: {
withAnimation {
self.global.showMenu.toggle()
}
}){
if self.global.showMenuButton {
Image(systemName: "line.horizontal.3")
.imageScale(.large)
.frame(width: 100, height: 60)
.background(Color.black)
.clipShape(Circle())
.opacity(0.8)
.foregroundColor(.white)
.rotationEffect(.degrees(self.global.showMenu ? 90 : 0))
.scaleEffect(self.global.showMenu ? 1.2 : 1)
} else {
EmptyView()
}
}
.padding(.top, -380)
}
}
SwiftUI has a zIndex() modifier that you can use to order the views.
This could help in your situation.
I want to display a label in the centre of a view with a progress indicator to the right of the label. How can I do this in SwiftUI on macOS?
The code below aligns the HStack in the centre of the VStack but I want the text centered and the progress indicator aligned with the text's trailing edge. I guess I could replace the HStack with a ZStack but it's still not clear how one aligns two controls to each other or how one prevents the container from be centered by its container.
import SwiftUI
struct AlignmentTestView: View {
var body: some View {
VStack(alignment: .center, spacing: 4) {
HStack {
Text("Some text")
ActivityIndicator()
}
}.frame(width: 200, height: 200)
.background(Color.pink)
}
}
struct AlignmentTestView_Previews: PreviewProvider {
static var previews: some View {
AlignmentTestView()
}
}
Here is possible approach (tested replacing ActivityIndicator with just circle).
Used Xcode 11.4 / iOS 13.4 / macOS 10.15.4
var body: some View {
VStack {
HStack {
Text("Some text")
.alignmentGuide(.hAlignment) { $0.width / 2.0 }
ActivityIndicator()
}
}
.frame(width: 200, height: 200, alignment:
Alignment(horizontal: .hAlignment, vertical: VerticalAlignment.center))
.background(Color.pink)
}
extension HorizontalAlignment {
private enum HAlignment : AlignmentID {
static func defaultValue(in d: ViewDimensions) -> CGFloat {
return d[HorizontalAlignment.center]
}
}
static let hAlignment = HorizontalAlignment(HAlignment.self)
}
I’m creating an app where I have create first screen (it will be short description of application) and on the screen I have a next Button if I click on next button it should be dismiss otherwise it must not be dismiss either pull down.
If user pull down a sheet, it should be again re-position.
The problem is, that the user can dismiss the modal by swiping it down and application dashboard screen show that should be prevented.
How can we prevent to dismiss the Model by pull down.
struct ModalView : View {
#Environment(\.presentationMode) var presentationMode
var body: some View {
Rectangle()
.fill(Color.orange)
.frame(width: 400, height: 650)
.overlay(
VStack{
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
HStack {
Image(systemName: "chevron.left")
Text("Dismiss")
}.padding(10.0)
.overlay(
RoundedRectangle(cornerRadius: 10.0)
.stroke(lineWidth: 2.0)
)
}.accentColor(.white)
})
.border(Color.blue)
.gesture( DragGesture())
}
}
ContentView
struct ContentView: View {
//MARK: Properties
//isPresented:- Present's a Welcome Screen in the form of cards.
#State private var isPresented = true
var body: some View {
VStack{
DashboardView()
.sheet(isPresented: $isPresented){
//IntroductionView(isPresentingSheet: self.$isPresented)
ModalView()
}
}
}
}
DashboardView
struct DashboardView: View {
var body: some View {
Text("Hello SwiftUI")
}
}
You can try this solution:
struct ModalWrapper: View {
var body: some View {
ModalView().highPriorityGesture(DragGesture())
}
}
struct ModalView : View {
#Environment(\.presentationMode) var presentationMode
var body: some View {
Rectangle()
.fill(Color.orange)
.frame(width: 400, height: 650)
.overlay(
VStack{
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
HStack {
Image(systemName: "chevron.left")
Text("Dismiss")
}.padding(10.0)
.overlay(
RoundedRectangle(cornerRadius: 10.0)
.stroke(lineWidth: 2.0)
)
}.accentColor(.white)
})
.border(Color.blue)
.highPriorityGesture(DragGesture())
}
}
struct ContentView: View {
//MARK: Properties
//isPresented:- Present's a Welcome Screen in the form of cards.
#State private var isPresented = true
var body: some View {
VStack{
DashboardView()
.sheet(isPresented: $isPresented){
//IntroductionView(isPresentingSheet: self.$isPresented)
ModalWrapper()
}
}
}
}
struct DashboardView: View {
var body: some View {
Text("Hello SwiftUI")
}
}
Here I have added ModalWrapper for wrap the modal view Or else you will have to add highPriorityGesture(DragGesture()) to all subviews of the ModalView So it is better to keep one wrapper view.
Hope this will help you.
I have created a round button with following code:
struct RoundButton : View {
var value = "..."
var body: some View {
GeometryReader { geometry in
Button(action: {}) {
VStack {
Text(self.value)
.font(.headline)
.color(Color("NightlyDark"))
Text("MIN")
.font(.footnote)
.color(.white)
}
.frame(width: geometry.size.width, height: geometry.size.height)
}
.clipShape(Circle())
}
}
}
But when clicking the button, the shape gets deformed. Any idea why this happens ?
Same happens when i use .mask(Circle())
Is this a beta thing or normal behavior ? And does anyone maybe know a better way to create rounded buttons ?
what happens here is Button Considers all screen[width + height] as their frame by default.
so you have to set Button also.
I think it's the default behavior in watchOS
Here Your Code :
struct RoundButton: View {
var value = "..."
var body: some View {
GeometryReader { geometry in
Button(action: {}) {
VStack {
Text(self.value)
.font(.headline)
.foregroundColor(Color("NightlyDark"))
Text("MIN")
.font(.footnote)
.foregroundColor(.white)
}
.frame(width: geometry.size.width, height: geometry.size.height)
}
.frame(width: geometry.size.width, height: geometry.size.height)
.clipShape(Circle())
}
}
}
Note: i'm using Xcode 11 Beta 4