Skip to main content
gabth u/gabth avatar

gabbb

u/gabth

Feed options
Hot
New
Top
View
Card
Compact

r/SwiftUI icon
A banner for the subreddit

For SwiftUI discussion, questions and showcasing SwiftUI is a UI development framework by Apple that lets you declare interfaces in an intuitive manner. Please keep content related to SwiftUI only. For Swift programming related content, visit r/Swift. For iOS programming related content, visit r/iOSProgramming


Weekly visitors Weekly contributions
r/SwiftUI
A banner for the subreddit

For SwiftUI discussion, questions and showcasing SwiftUI is a UI development framework by Apple that lets you declare interfaces in an intuitive manner. Please keep content related to SwiftUI only. For Swift programming related content, visit r/Swift. For iOS programming related content, visit r/iOSProgramming


Weekly visitors Weekly contributions

[deleted by user]

gabth
commented

It seems indeed that button's gesture interferes with the scrollview's gesture. However, you could achieve a similar result using the .simultaneousGesture modifier with DragGesture on the button. I've put together some demo code:

@State private var scale: CGFloat = 1

var body: some View {
    ScrollView {
        VStack {
            Button {
                
            } label: {
                Image(systemName: "star")
            }
            .buttonStyle(.plain)
            .padding()
            .background(.red)
            .scaleEffect(scale)
            .simultaneousGesture(
                DragGesture(minimumDistance: 0)
                    .onChanged({ _ in
                        withAnimation(.bouncy(duration: 0.25)) {
                            scale = 0.9
                        }
                    })
                    .onEnded({ _ in
                        withAnimation(.bouncy(duration: 0.4)) {
                            scale = 1.0
                        }
                    })
            )
        }
    }
}

You can fine-tune the animation as you see it fits. If you want to apply the same effect to several buttons create a custom view modifier including the repeated modifiers:

struct MyButtonModifiers: ViewModifier {
    @State private var scale: CGFloat = 1
    
    func body(content: Content) -> some View {
        content
            .buttonStyle(.plain)
            .padding()
            .background(.red)
            .scaleEffect(scale)
            .simultaneousGesture(
                DragGesture(minimumDistance: 0)
                    .onChanged({ _ in
                        withAnimation(.bouncy(duration: 0.25)) {
                            scale = 0.9
                        }
                    })
                    .onEnded({ _ in
                        withAnimation(.bouncy(duration: 0.4)) {
                            scale = 1.0
                        }
                    })
            )
    }
}


ScrollView {
    VStack {
        Button {
            
        } label: {
            Image(systemName: "star")
        }
        .modifier(MyButtonModifiers())
    }
}

You can create a View extension for a more integrated use:

extension View {
    func myButtonModifiers() -> some View {
        self.modifier(MyButtonModifiers())
    }
}


Button {
    
} label: {
    Image(systemName: "star")
}
.myButtonModifiers()

I hope this alternative helps!


r/SwiftUI icon
A banner for the subreddit

For SwiftUI discussion, questions and showcasing SwiftUI is a UI development framework by Apple that lets you declare interfaces in an intuitive manner. Please keep content related to SwiftUI only. For Swift programming related content, visit r/Swift. For iOS programming related content, visit r/iOSProgramming


Weekly visitors Weekly contributions
r/SwiftUI
A banner for the subreddit

For SwiftUI discussion, questions and showcasing SwiftUI is a UI development framework by Apple that lets you declare interfaces in an intuitive manner. Please keep content related to SwiftUI only. For Swift programming related content, visit r/Swift. For iOS programming related content, visit r/iOSProgramming


Weekly visitors Weekly contributions

How to change the size of Swift default Menu and its position

gabth
commented

I'm afraid that what you're trying to do is not possible using the built-in menu view. Menu appears automatically above or below the button that triggers it (it's up to the system to define that) and its width depends on the content. If you want a modal menu that appears below the button and occupies the full width, then you should probably come up with your own custom implementation.

I don't know if it helps, but you could check out the DisclosureGroup view. It has initializers that get an "isExpanded" argument that allows to expand and collapse, showing and hiding subviews on tap. Expanded state can be changed programmatically too.


r/SwiftUI icon
A banner for the subreddit

For SwiftUI discussion, questions and showcasing SwiftUI is a UI development framework by Apple that lets you declare interfaces in an intuitive manner. Please keep content related to SwiftUI only. For Swift programming related content, visit r/Swift. For iOS programming related content, visit r/iOSProgramming


Weekly visitors Weekly contributions
r/SwiftUI
A banner for the subreddit

For SwiftUI discussion, questions and showcasing SwiftUI is a UI development framework by Apple that lets you declare interfaces in an intuitive manner. Please keep content related to SwiftUI only. For Swift programming related content, visit r/Swift. For iOS programming related content, visit r/iOSProgramming


Weekly visitors Weekly contributions

SwiftUI tapgesture

gabth
commented

You don't need to use the .onTapGesture with TabView. When selecting a tab it automatically returns to the root view of that tab. However, if you're trying to achieve something different then please clarify. Documentation from Apple might be helpful: https://developer.apple.com/documentation/swiftui/tabview


r/swift icon
A banner for the subreddit

Swift is a general-purpose programming language built using a modern approach to safety, performance, and software design patterns.


Weekly visitors Weekly contributions
r/swift
A banner for the subreddit

Swift is a general-purpose programming language built using a modern approach to safety, performance, and software design patterns.


Weekly visitors Weekly contributions

[deleted by user]

gabth
replied to _sourDiesel

Based on your screenshot I guess that your app is not running. Run the app and check again. Apologies if my conclusion is wrong and the app is running. In that case make sure that an instance of the ViewController class is actually created.


r/swift icon
A banner for the subreddit

Swift is a general-purpose programming language built using a modern approach to safety, performance, and software design patterns.


Weekly visitors Weekly contributions
r/swift
A banner for the subreddit

Swift is a general-purpose programming language built using a modern approach to safety, performance, and software design patterns.


Weekly visitors Weekly contributions

Why does this not work?

gabth
commented

Try to change the function's result type to Double. The division you're performing in it will not return Int always.



r/SwiftUI icon
A banner for the subreddit

For SwiftUI discussion, questions and showcasing SwiftUI is a UI development framework by Apple that lets you declare interfaces in an intuitive manner. Please keep content related to SwiftUI only. For Swift programming related content, visit r/Swift. For iOS programming related content, visit r/iOSProgramming


Weekly visitors Weekly contributions
r/SwiftUI
A banner for the subreddit

For SwiftUI discussion, questions and showcasing SwiftUI is a UI development framework by Apple that lets you declare interfaces in an intuitive manner. Please keep content related to SwiftUI only. For Swift programming related content, visit r/Swift. For iOS programming related content, visit r/iOSProgramming


Weekly visitors Weekly contributions

"Windows" was deprecated in iOS 15, any help on what/how to replace it?

gabth
replied to rosone

I'm glad, you're welcome!


r/SwiftUI icon
A banner for the subreddit

For SwiftUI discussion, questions and showcasing SwiftUI is a UI development framework by Apple that lets you declare interfaces in an intuitive manner. Please keep content related to SwiftUI only. For Swift programming related content, visit r/Swift. For iOS programming related content, visit r/iOSProgramming


Weekly visitors Weekly contributions
r/SwiftUI
A banner for the subreddit

For SwiftUI discussion, questions and showcasing SwiftUI is a UI development framework by Apple that lets you declare interfaces in an intuitive manner. Please keep content related to SwiftUI only. For Swift programming related content, visit r/Swift. For iOS programming related content, visit r/iOSProgramming


Weekly visitors Weekly contributions

"Windows" was deprecated in iOS 15, any help on what/how to replace it?

gabth
commented

I've written a post about how to present email composer in SwiftUI using a custom view modifier. If you want to take a look, here is the link:

https://gabth.medium.com/composing-emails-in-swiftui-using-a-view-modifier-a764a981080a

The idea is to create a UIViewControllerRepresentable custom type that deals with the MFMailComposeViewController, and present an instance of it in a sheet creating a custom and reusable view modifier.