티스토리 뷰

https://developer.apple.com/documentation/swiftui/scrollviewproxy/scrollto(_:anchor:)

ScrollViewReader를 이용해 proxy.scrollTo(id, anchor)를 이용하면 지정한 id의 뷰로 스크롤되어 이동할 수 있다.

func scrollTo<ID>(
    _ id: ID,
    anchor: UnitPoint? = nil
) where ID : Hashable
ScrollViewReader { proxy in 
	ScrollView {
    	Foreach(contents) { content in
        	ExampleView()
            	.id(content.id)
        }
    }
}
Button("이동") {
	proxy.scrollTo(이동하고싶은id, anchor: .leading)
}

 

여기서 anchor의 경우 UnitPoint타입이고, 다음과 같이 미리 정의되어 있어서 편하게 사용할 수 있다.

UnitPoint.zero // UnitPoint(x: 0, y: 0)
UnitPoint.center // UnitPoint(x: 0.5, y: 0.5)
UnitPoint.leading // UnitPoint(x: 0, y: 0.5)
UnitPoint.trailing // UnitPoint(x: 1, y: 0.5)
UnitPoint.top // UnitPoint(x: 0.5, y: 0)
UnitPoint.bottom // UnitPoint(x: 0.5, y: 1)
UnitPoint.topLeading // UnitPoint(x: 0, y: 0)
UnitPoint.topTrailing // UnitPoint(x: 1, y: 0)
UnitPoint.bottomLeading // UnitPoint(x: 0, y: 1)
UnitPoint.bottomTrailing // UnitPoint(x: 1, y: 1)

 

 

나의 경우, .leading으로 이동하되 옆에 약간의 공간을 두고 싶었다. 근데 .leading으로 하면 아예 공간이 남지 않고 딱 붙은 형태로 출력된다.

이때는 anchor의 UnitPoint를 미리 정의된 형태가 아니라 새로 정의해주면 된다.

ex) UnitPoint(x: 0.05, y: 0.5)

 

 

'iOS' 카테고리의 다른 글

[Error] tuist fetch할때 오류  (0) 2023.11.06
[Error] Xcode "Cycle inside ...; ~ 오류  (0) 2023.10.27
[Swift/iOS] JSONSerialization  (0) 2023.09.29
[ReactorKit] ReactorKit의 transform  (0) 2023.09.15
[Swift Concurrency] Actor - isolation(격리)  (2) 2023.07.08
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/06   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
글 보관함