How do I get a DisclosureGroup to scroll up when it is expanded and part of it goes off the screen?

I have a .sheet that launches when a button is pushed, and it displays a DisclosureGroup which has a List of items, and at the very bottom a DisclosureGroup which has some text and a Button:

struct TestView: View
{
    @State private var sheetIsPresented: Bool = false
    @State private var outerDisclosureIsExpanded: Bool = false
    @State private var innerDisclosureIsExpanded: Bool = false
    
    var body: some View
    {
        Button
        {
            self.sheetIsPresented = true
        }
        label:
        {
            Label("Add Item", systemImage: "plus")
        }
        .sheet(isPresented: self.$sheetIsPresented)
        {
            VStack
            {
                Form
                {
                    Section(header: Text("Section"))
                    {
                        DisclosureGroup("Outer Disclosure Group", isExpanded: self.$outerDisclosureIsExpanded)
                        {
                            ForEach(1...20, id: .self)
                            {
                                index in Text("Value (index)")
                            }
                            DisclosureGroup("Inner Disclosure Group", isExpanded: self.$innerDisclosureIsExpanded)
                            {
                                Text("Text")
                                
                                Button("Button")
                                {
                                    print("Button Pressed")
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

The .sheet seems to be a ScrollView by default because when the DisclosureGroup has a list that’s longer than can be displayed on the screen, you can scroll up to see the bottom. I would like to make it so that when the inner DisclosureGroup is expanded and its items are off screen, that the ScrollView scrolls up so that the full contents of hte inner DisclosureGroup can be seen, like so:

I’ve tried wrapping the VStack in a ScrollViewReader with proxy and ScrollView, setting the id of the Button within the inner DisclosureGroup and doing:

withAnimation 
{
     proxy.scrollTo("Button", anchor: .top)
}

But it changed the formatting of my List and also did not work. Does anyone know how I can accomplish this?

As you mentioned scrolling can be achieved by using ScrollViewReader. Keeping ScrollViewReader as the outermost view does not affect the list formatting. Add id to button and use the same id in scrollTo method.

ScrollViewReader { proxy in
    Form {
        Section(header: Text("Section")) {
            DisclosureGroup("Outer Disclosure Group", isExpanded: self.$outerDisclosureIsExpanded) {
                ForEach(1...20, id: .self) {
                    index in Text("Value (index)")
                }
                DisclosureGroup("Inner Disclosure Group", isExpanded: self.$innerDisclosureIsExpanded) {
                    Text("Text")
                    
                    Button("Button") {
                        print("Button Pressed")
                    }
                    .id("ButtonID")
                }
            }
        }
    }
    .onChange(of: self.innerDisclosureIsExpanded) { _, newValue in
        if innerDisclosureIsExpanded {
            withAnimation {
                proxy.scrollTo("ButtonID")
            }
        }
    }
}

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

How do I get a DisclosureGroup to scroll up when it is expanded and part of it goes off the screen?

I have a .sheet that launches when a button is pushed, and it displays a DisclosureGroup which has a List of items, and at the very bottom a DisclosureGroup which has some text and a Button:

struct TestView: View
{
    @State private var sheetIsPresented: Bool = false
    @State private var outerDisclosureIsExpanded: Bool = false
    @State private var innerDisclosureIsExpanded: Bool = false
    
    var body: some View
    {
        Button
        {
            self.sheetIsPresented = true
        }
        label:
        {
            Label("Add Item", systemImage: "plus")
        }
        .sheet(isPresented: self.$sheetIsPresented)
        {
            VStack
            {
                Form
                {
                    Section(header: Text("Section"))
                    {
                        DisclosureGroup("Outer Disclosure Group", isExpanded: self.$outerDisclosureIsExpanded)
                        {
                            ForEach(1...20, id: .self)
                            {
                                index in Text("Value (index)")
                            }
                            DisclosureGroup("Inner Disclosure Group", isExpanded: self.$innerDisclosureIsExpanded)
                            {
                                Text("Text")
                                
                                Button("Button")
                                {
                                    print("Button Pressed")
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

The .sheet seems to be a ScrollView by default because when the DisclosureGroup has a list that’s longer than can be displayed on the screen, you can scroll up to see the bottom. I would like to make it so that when the inner DisclosureGroup is expanded and its items are off screen, that the ScrollView scrolls up so that the full contents of hte inner DisclosureGroup can be seen, like so:

I’ve tried wrapping the VStack in a ScrollViewReader with proxy and ScrollView, setting the id of the Button within the inner DisclosureGroup and doing:

withAnimation 
{
     proxy.scrollTo("Button", anchor: .top)
}

But it changed the formatting of my List and also did not work. Does anyone know how I can accomplish this?

As you mentioned scrolling can be achieved by using ScrollViewReader. Keeping ScrollViewReader as the outermost view does not affect the list formatting. Add id to button and use the same id in scrollTo method.

ScrollViewReader { proxy in
    Form {
        Section(header: Text("Section")) {
            DisclosureGroup("Outer Disclosure Group", isExpanded: self.$outerDisclosureIsExpanded) {
                ForEach(1...20, id: .self) {
                    index in Text("Value (index)")
                }
                DisclosureGroup("Inner Disclosure Group", isExpanded: self.$innerDisclosureIsExpanded) {
                    Text("Text")
                    
                    Button("Button") {
                        print("Button Pressed")
                    }
                    .id("ButtonID")
                }
            }
        }
    }
    .onChange(of: self.innerDisclosureIsExpanded) { _, newValue in
        if innerDisclosureIsExpanded {
            withAnimation {
                proxy.scrollTo("ButtonID")
            }
        }
    }
}

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

How do I get a DisclosureGroup to scroll up when it is expanded and part of it goes off the screen?

I have a .sheet that launches when a button is pushed, and it displays a DisclosureGroup which has a List of items, and at the very bottom a DisclosureGroup which has some text and a Button:

struct TestView: View
{
    @State private var sheetIsPresented: Bool = false
    @State private var outerDisclosureIsExpanded: Bool = false
    @State private var innerDisclosureIsExpanded: Bool = false
    
    var body: some View
    {
        Button
        {
            self.sheetIsPresented = true
        }
        label:
        {
            Label("Add Item", systemImage: "plus")
        }
        .sheet(isPresented: self.$sheetIsPresented)
        {
            VStack
            {
                Form
                {
                    Section(header: Text("Section"))
                    {
                        DisclosureGroup("Outer Disclosure Group", isExpanded: self.$outerDisclosureIsExpanded)
                        {
                            ForEach(1...20, id: .self)
                            {
                                index in Text("Value (index)")
                            }
                            DisclosureGroup("Inner Disclosure Group", isExpanded: self.$innerDisclosureIsExpanded)
                            {
                                Text("Text")
                                
                                Button("Button")
                                {
                                    print("Button Pressed")
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

The .sheet seems to be a ScrollView by default because when the DisclosureGroup has a list that’s longer than can be displayed on the screen, you can scroll up to see the bottom. I would like to make it so that when the inner DisclosureGroup is expanded and its items are off screen, that the ScrollView scrolls up so that the full contents of hte inner DisclosureGroup can be seen, like so:

I’ve tried wrapping the VStack in a ScrollViewReader with proxy and ScrollView, setting the id of the Button within the inner DisclosureGroup and doing:

withAnimation 
{
     proxy.scrollTo("Button", anchor: .top)
}

But it changed the formatting of my List and also did not work. Does anyone know how I can accomplish this?

As you mentioned scrolling can be achieved by using ScrollViewReader. Keeping ScrollViewReader as the outermost view does not affect the list formatting. Add id to button and use the same id in scrollTo method.

ScrollViewReader { proxy in
    Form {
        Section(header: Text("Section")) {
            DisclosureGroup("Outer Disclosure Group", isExpanded: self.$outerDisclosureIsExpanded) {
                ForEach(1...20, id: .self) {
                    index in Text("Value (index)")
                }
                DisclosureGroup("Inner Disclosure Group", isExpanded: self.$innerDisclosureIsExpanded) {
                    Text("Text")
                    
                    Button("Button") {
                        print("Button Pressed")
                    }
                    .id("ButtonID")
                }
            }
        }
    }
    .onChange(of: self.innerDisclosureIsExpanded) { _, newValue in
        if innerDisclosureIsExpanded {
            withAnimation {
                proxy.scrollTo("ButtonID")
            }
        }
    }
}

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật