4
votes

I am trying to position a view absolutely inside a other view in SwiftUI for MacOS.

I am basically having a VStack, with a ScrollView/ List inside. How can I position a view absolute in a corner. Even when the window gets shrinked it should stay there. It even may overlap with the content of the ListView.

I couldn't really figure it out how to do it. I used a different approach at the moment, with using a new VStack then use Spacer() and put a new HStack right at the bottom with an item aligned right. This works but basically don't overlap. The views are separated.

Here is a demo... I want to achieve that yellow circle. It can basically be an Image or another View..

enter image description here

Even when the window gets smaller, it will stay there:

enter image description here

Here is the code I used... it is just a simple scroll view with content.

    ScrollView(content: {


        HStack
        {
            Spacer()
            Text("Test Test TestTest Test TestTest Test Test")
            Spacer()
        }
        ...




    }).frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color(.red))
1

1 Answers

5
votes

It can be used ZStack as below

struct TestCircleWithList: View {
    var body: some View {
        ZStack(alignment: .bottomTrailing) {
            ScrollView(content: {
                HStack
                {
                    Spacer()
                    Text("Test Test TestTest Test TestTest Test Test")
                    Spacer()
                }
            }).frame(maxWidth: .infinity, maxHeight: .infinity)
                .background(Color(.red))
            Circle().fill(Color.yellow)
                .frame(width: 80, height: 80)
                .padding(20)
        }
    }
}