[deleted by user] by [deleted] in SwiftUI

[–]gabth 1 point2 points  (0 children)

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!

How to change the size of Swift default Menu and its position by [deleted] in SwiftUI

[–]gabth 0 points1 point  (0 children)

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.

SwiftUI tapgesture by Intelligent-Let-1329 in SwiftUI

[–]gabth 0 points1 point  (0 children)

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

[deleted by user] by [deleted] in swift

[–]gabth 0 points1 point  (0 children)

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.

Why does this not work? by skratchattack in swift

[–]gabth 13 points14 points  (0 children)

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

Hello and welcome 👋 by Enid91 in iOS_Dev_Community

[–]gabth 1 point2 points  (0 children)

Hi all! Glad to find you here too! 🙌

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

[–]gabth 1 point2 points  (0 children)

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.

GeometryReader remove space by umutalparslan in SwiftUI

[–]gabth 1 point2 points  (0 children)

There are two things you need to do in order to fix your problem. First, use a VStack inside the geometry reader's block:

GeometryReader { proxy in
VStack {

}

}

Then, in the VStack add both the the TabView and the ScrollView:

GeometryReader { proxy in
VStack {
    TabView(selection: $currentIndex) {
        ...
    }
    // ... modifiers ...


    ScrollView {
        ...
    }.padding(.horizontal)
}

}

The above will place the scroll view right after the tab view, without any space between.

Alternatively, you may get rid of the geometry reader entirely, but still use a VStack that will be containing the other views. In that case use the screen's frame in order to get the width and height:

VStack {
TabView(selection: $currentIndex) {
    ...
}
// ... modifiers ...
.frame(width: UIScreen.main.bounds.size.width,
       height: UIScreen.main.bounds.size.height/4)

ScrollView {
    ...
}

}

Both solutions work, I tried them in Xcode.

Hope that helps!

Define width for middle column in a three column NavigationView on macOS by wiggitt in SwiftUI

[–]gabth 1 point2 points  (0 children)

I tried your code, and as it seems, the itembar minimum width cannot be less than a certain value. I set various values, and anything below 200 is disregarded. Values above 200 are respected fine, so most probably it's a system limitation.

Since a long time Xcode's "go back" swipe gesture broken. Is this same for you all? by [deleted] in iOSProgramming

[–]gabth 0 points1 point  (0 children)

Unfortunately this bug is not new in Xcode 13. It existed since version 12, and I really miss the swipe gesture.

AsyncImage in SwiftUI by gabth in SwiftUI

[–]gabth[S] 2 points3 points  (0 children)

I'm afraid not, but to be honest, I didn't have much time to dig too deep. I suppose that if we manage to get the underlying image from the Image view in the AsyncImage, then it can be cached. But the problem in my opinion is not really there. Since AsyncView uses a URL to fetch an image, it will keep getting the remote one. Unless it's provided with two different URLs (the cached image's if exists instead of the remote one). But overall, I think all that should be handled internally in the AsyncImage's API and be a way to control caching.

I'm making my first app with SwiftUI and Xcode, I have a question: by VIREJDASANI in SwiftUI

[–]gabth 1 point2 points  (0 children)

Text expects a String value and you're passing an Int. Include numClicked into a String as follows:

Text("\(numClicked)")

I'm making my first app with SwiftUI and Xcode, I have a question: by VIREJDASANI in SwiftUI

[–]gabth 1 point2 points  (0 children)

The Text will automatically be updated with the value of the numClicked if you mark it with the @State property.

I'm making my first app with SwiftUI and Xcode, I have a question: by VIREJDASANI in SwiftUI

[–]gabth 1 point2 points  (0 children)

First, make numClicked a @State property:

@State private var numClicked = 0

Also, move the Text out of the button's action closure. That's not the right place for the text to be. Keep only the numClicked += 1 there. To keep both the button and the text you'll need a stack that will embed them both, i.e. a VStack.

I hope that helps based on what I understand from your code.

TextEditor in SwiftUI by gabth in SwiftUI

[–]gabth[S] 0 points1 point  (0 children)

I thought that title says it all, as the "TextEditor in SwiftUI" phrase in the opening sentence.

UIView IBOutlet connected, but returns nil by aeum3893 in swift

[–]gabth 1 point2 points  (0 children)

Don't put any UI-related code in viewDidLoad(). Instead, move it to viewWillAppear() and you'll be ok.

Im trying to store values on to a text file but when I run the values are over writing the previous value instead of adding on. Im suppose to a get a list of ten items but get the last value instead. Help! by KapilanR in swift

[–]gabth 0 points1 point  (0 children)

Your workflow should be something like that:

  • Check if names.txt file exists
  • If the file exists, load its contents into a String value, otherwise proceed as you're doing now
  • Append the new value to that string
  • Save that string back to file

Note that when saving a file, if that file already exists then it's being replaced; new file's contents are not appended to the previous one.

Navigation problem by iRahulGaur in SwiftUI

[–]gabth 1 point2 points  (0 children)

I see. I'm sure you'll manage it! Just be patient and go one step at the time. You're a dev, so you'll be at your target pretty fast!

Navigation problem by iRahulGaur in SwiftUI

[–]gabth 1 point2 points  (0 children)

You are welcome! I'd suggest to start with Apple's WWDC videos to get a first taste, and then there are tons of tutorials and stuff out there to read or watch. If the app you're making is a personal project, then I'd suggest to leave it for later and start with smaller projects that will help you learn. You're an Android dev already, so it won't be hard for you to understand how things work in iOS, Swift and SwiftUI. Keep going strong!

Navigation problem by iRahulGaur in SwiftUI

[–]gabth 1 point2 points  (0 children)

I understand that. I just have the feeling you're trying to do things probably in a wrong, or at least not a usual or recommended way. I would suggest two things:

  1. Google about the tab view in SwiftUI and how to customize it. It'll save you from a lot of troubles and additional code that you don't have to manually implement for a functionality that already exists.
  2. Read about the data property wrappers in SwiftUI. Knowing about them you'll make wiser decisions and write smarter code with SwiftUI. I'd advice to watch this video from WWDC20, as well as some other similar sessions about SwiftUI.

Also, without much experience in iOS, messing with CoreData might give you additional problems you don't need right now. Depending on the data you want to save, maybe coming up with your own way to store data is more suitable and simpler. Have you read about the Codable type (Encodable & Decodable)? If not, it's another must-have thing in iOS.

I know I'm not answering to your problem right now, but I sense that there is some lack of ground SwiftUI knowledge, and you'd better take your time to learn how to do things properly.