简体   繁体   English

如何给出一个固定的 position 视图,使其不受 SwiftUI 中的滚动影响

[英]How to give a view a fixed position so it is not affected by scroll in SwiftUI

Is there a way in SwiftUI to give any view object a fixed position so that it doesn't move when the rest of the view scrolls? SwiftUI 中有没有办法给任何视图 object 一个固定的 position 以便它在 Z65E88060B5C62AF62AAD8 滚动时不会移动?

      VStack.init {
            VStack.init(alignment: .center, spacing: 0, content: {
                Text("UnScrollable")
            }).padding()
            
            ScrollView.init {
                VStack {
                    Text("Scrollable")
                }
            }
        }

you'll need to set your frame on the first VStack because if vstack somehow needed more space than current height resolution your scrollview wont visible to the screen.你需要在第一个 VStack 上设置你的框架,因为如果 vstack 需要比当前高度分辨率更多的空间,你的滚动视图将不会在屏幕上可见。

if you need fixed position like floating button for example you need to use this logic ZStack -> VStack/View With position -> ScrollView you'll need button with custom position for make this to happen,如果您需要固定 position 之类的浮动按钮,例如,您需要使用此逻辑ZStack -> VStack/View With position -> ScrollView您需要带有自定义 position 的按钮才能实现这一点,

var body: some View {
    GeometryReader.init { reader in
        ZStack {
            
            ScrollView.init {
                VStack {
                    Text.init("Hello?")
                    Rectangle.init().foregroundColor(.orange).frame(width: reader.size.width, height: 1024, alignment: .center)
                }
            }
            
            VStack.init {
                Text("wakwkakwkaw").position(.init(x: reader.size.width - 100, y: reader.size.height - 100))
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM