Learn Courses My Dashboard

Removing an Object From the View

Hello there! Welcome to my first post on this forum! I am having an issue with Xcode 12 with removing an object from the view. My app runs on a lot of menus, so I need to change each one for the options to sync with different functions. (If anyone knows how to change the function of a Button within a Menu, please let me know. That would make my life a lot easier.) Anyway, both menus are placed in a VStack. They have to be to line up with the text above. I ran into the issue earlier today when I tested the app. Even though the first menu used has disappeared, it is below the invisible menu. As I continue with the steps, the choose button will continue to get lower. Here is an example of how I would make it “invisible”:

// All menus are text-oriented, so I just change the text property.

@State var menuTitle = "Choose"

Menu(menutitle) {

// Buttons would go here

}

// Making menu invisible

menuTitle = ""

As you can see, the menu now has no title text, making it invisible and impossible to click on, however, it still takes up space in the view. Does anyone know how to fix this? I thought there were ways to do this with previous versions of iOS, but I can’t find anything now.

Share your code for the entire view so that there is context in what you are trying to do.

Paste your code in as text, rather than providing a screenshot.

Place 3 back-ticks ``` on the line above your code and 3 back-ticks ``` on the line below your code so that it is formatted nicely. The back-tick character is located on the same keyboard key as the tilde character ~.

This also makes it easier for anyone assisting as they can copy the code and carry out some testing rather than having to type the code in from an image.

1 Like

Thanks for letting me know! I’ll list it below because it does not appear that I can edit posts on this forum.

Full View Code
//
//  ContentView.swift
//  Scam Protector
//
//  Created by Hendric Voris on 4/11/21.
//

import SwiftUI

struct ContentView: View {
    
    @State var percent = 0
    @State var percentText = ""
    @State var nextButtonText = "Start"
    @State var nextButtonImage = "play.fill"
    @State var QuestionText = ""
    @State var QuestionNum = 0
    @State var numTypos = 0.0
    @State var stepNum = 0
    @State var ageNum = 0
    @State var temporaryTypoPercent = 0.0
    
    //Generic menu variables
    
    @State var menuTypoTitle = ""
    @State var menuAgeTitle = ""
    
    //Typo variables
    
    @State var OneToFive = ""
    @State var SixToTen = ""
    @State var Eleven = ""
    @State var TwentyUp = ""
    
    // Age variables
    
    @State var highAgeVar = ""
    @State var mediumAgeVar = ""
    @State var lowAgeVar = ""
    
    
    // Functions
    
    func removeAllText() {
        menuTypoTitle = ""
        menuAgeTitle = ""
        OneToFive = ""
        SixToTen = ""
        Eleven = ""
        TwentyUp = ""
    }
    
    // Functions for age menu
    
    func calculateAgePercent() {
        let double = Double(ageNum)
        let agePercent = double * 1.5
        temporaryTypoPercent = agePercent
    }
    
    func LowAge() {
        numTypos = 6
        calculateAgePercent()
    }
    
    func mediumAge() {
        ageNum = Int(1.5)
        let agePercent = ageNum * Int(1.5)
        percent += Int(agePercent)
        percentText = String(percent) + "%"
    }
    
    func highAge() {
        ageNum = 1
        let agePercent = ageNum * Int(1.5)
        percent += Int(agePercent)
        percentText = String(percent) + "%"
    }
    
    // Functions for typo menu
    
    func calculateTypoPercent() {
       let typoPercent = numTypos / 2
       percent += Int(typoPercent)
       percentText = String(percent) + "%"
    }
    
    func changeTypoMenu() {
        //Changes variables for menu text
        
        menuTypoTitle = "Choose"
        OneToFive = "1-5"
        SixToTen = "6-10"
        Eleven = "11-15"
        TwentyUp = "20+"
    }
    
    func changeTypoTo5() {
        numTypos = 5
        calculateTypoPercent()
    }
    
    func changeTypoTo10() {
        numTypos = 10
        calculateTypoPercent()
    }
    
    func changeTypoTo15() {
        numTypos = 15
        calculateTypoPercent()
    }
    
    func highNumTypos() {
        numTypos = 20
        calculateTypoPercent()
    }
    
    var body: some View {
    
        
        VStack {
            
            // Instances to use later
            
        Text("Scam Inspector")
            .fontWeight(.heavy).font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/).foregroundColor(.green).bold()
       
          
            
            HStack {
            
            VStack {
                
               
                
                // Question
                
                Text(percentText).font(.title2).bold().foregroundColor(.black)
                Text(QuestionText).padding(.top, 90.866).font(.title).foregroundColor(.green).transition(.slide)
               
                // Menus
               
                
                Menu(menuTypoTitle) { // Menu for typos
                    Button(OneToFive, action: changeTypoTo5)
                    Button(SixToTen, action: changeTypoTo10)
                    Button(Eleven, action: changeTypoTo15)
                    Button(TwentyUp, action: highNumTypos)
                }.animation(.easeOut)
                
                Menu(menuAgeTitle) { // Menu for age
                    Button(lowAgeVar, action: LowAge)
                    Button(mediumAgeVar, action: mediumAge)
                    Button(highAgeVar, action: highAge)
                }
               
                HStack {
                
                    
                  
                    
                    
                }
                
                                
               
                
                

                Spacer()
                Button(action: {
                    
                    print("Starting process")
                    
                    
                    // If statements to check what stage
                    
                    if stepNum == 0 {
                        stepNum = 1
                        
                        // Change view properties
                        changeTypoMenu()
                        QuestionText = "By estimation, how many typos have they had in the first couple messages?"
                        nextButtonImage = "play"
                        nextButtonText = "Next"
                        percentText = String(percent) + "%"
                        
                    } else if stepNum == 1 {
                        
                        stepNum = 2
                        
                        removeAllText()
                        
                        QuestionText = "Now it is time to their Roblox and Discord profiles! Click NEXT when you are ready to proceed!"
                    
                   
                        
                    } else if stepNum == 2 {
                        stepNum = 3
                        
                        removeAllText()
                        
                        QuestionText = "Look at their Roblox profile! How old is it?"
                        
                        menuAgeTitle = "Choose"
                        
                        lowAgeVar = "6 Months or Less"
                        
                        mediumAgeVar = "1-2 Years"
                        
                        highAgeVar = "3+ Years"
                        
                        
                    } else if stepNum == 3 {
                        stepNum = 4
                        
                        removeAllText()
                        
                        QuestionText = "Look at their Discord profile! How old is it?"
                        
                        menuAgeTitle = "Choose"
                        
                        lowAgeVar = "6 Months or Less"
                        
                        mediumAgeVar = "1-2 Years"
                        
                        highAgeVar = "3+ Years"
                        
                        
                    } else if stepNum == 4 {
                        stepNum = 5
                        
                        removeAllText()
                        
                        QuestionText = "Look at their Developer Forum profile! How old is it?"
                        
                        menuAgeTitle = "Choose"
                        
                        lowAgeVar = "6 Months or Less"
                        
                        mediumAgeVar = "1-2 Years"
                        
                        highAgeVar = "3+ Years"
                    }
                    
                    
                   
                    
                    
                    
                }, label: {
                    Image(systemName: nextButtonImage)
                })
                Text(nextButtonText).foregroundColor(.green)
            }.accentColor(.green)
            
            }
        
        }
        .padding(.top, 5.7)
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            ContentView()
            ContentView()
        }
            
            
    }
}
}

My main issue here is:

 Menu(menuTypoTitle) { // Menu for typos
                    Button(OneToFive, action: changeTypoTo5)
                    Button(SixToTen, action: changeTypoTo10)
                    Button(Eleven, action: changeTypoTo15)
                    Button(TwentyUp, action: highNumTypos)
                }.animation(.easeOut)
                
                Menu(menuAgeTitle) { // Menu for age
                    Button(lowAgeVar, action: LowAge)
                    Button(mediumAgeVar, action: mediumAge)
                    Button(highAgeVar, action: highAge)
                }

These two menus need to be separate because I need to have separate actions for each button, as well as a different number of actions. I am trying to figure out how to remove one menu from the view to make room for another, as the Age Menu gets placed below the one for typos, even though the Typo Menu does not show up because the text is set to "".

What you could do is have a boolean related to a specific Menu that determines if the menu should be shown.

For example, set a State variable like this:

@State private var isShowingMenuTypo = false
@State private var isShowingMenuAge = false

Then in your body code have an if statement around each of the menus that when it is true shows that menu View. You would set that boolean in your code that determines which menu to show by setting the boolean to true or false. You could have as many menus configured as you want by using that approach.

 if isShowingMenuTypo {
     Menu("Choose") { // Menu for typos
         Button(OneToFive, action: changeTypoTo5)
         Button(SixToTen, action: changeTypoTo10)
         Button(Eleven, action: changeTypoTo15)
         Button(TwentyUp, action: highNumTypos)
     }.animation(.easeOut)
 }
 
 if isShowingMenuAge {
     Menu("Choose") { // Menu for age
         Button(lowAgeVar, action: LowAge)
         Button(mediumAgeVar, action: mediumAge)
         Button(highAgeVar, action: highAge)
     }
 }
1 Like

Thank you! I knew there was something I could do, but I couldn’t think of it for the life of me.