Home > AI > IOS > SwiftUI >

Floating action button

Example 1: floating action button

struct FloatingMenuView: View {
    var body: some View {
        Button(action: {
            print("Show Menu")
        }) {
            Image(systemName: "plus.circle.fill")
                .resizable()
                .frame(width: 50, height: 50)
                .foregroundColor(Color(red: 153/255, green: 102/255, blue: 255/255))
        }
    }
}

ContentView.swift

struct ContentView: View {
    var body: some View {
            ZStack (alignment: .bottomTrailing){
                // this view needs to span all screen to achieve the effect
                Rectangle()
                    .foregroundColor(.white)
                    .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
            
                FloatingMenuView()
                    .padding()
        }
    }
}

Example 2: Linear gradient floating action button

struct ContentView: View {
    var body: some View {
        
        ZStack (alignment: .bottomTrailing){
            Rectangle()
                .foregroundColor(.white)
                .frame(maxWidth: .infinity, maxHeight: .infinity)
            
            LinearGradient(gradient: Gradient(colors: [.green, .blue]), startPoint: .top, endPoint: .bottom)
              .mask(Image(systemName: "plus.circle.fill")
                .resizable()
                .aspectRatio(contentMode: .fit)
              )
                .frame(width: 50, height: 50)
                .padding()
        }
    }
}

Leave a Reply