SwiftUI 使用 safeAreaInset 实现底部悬浮视图无法忽略安全区如何解决?
解法
问题原因
ignoresSafeArea(edges: .bottom)加在safeAreaInset闭包内部的子视图上不会生效,因为safeAreaInset修饰符本身会默认将其传入的自定义内容限制在对应边的安全区域以内,内部子视图的忽略安全区设置无法突破外层容器的限制。
调整方案
将ignoresSafeArea(edges: .bottom)修饰符移动到safeAreaInset的外层即可,调整后代码如下:
struct ContentView: View { var body: some View { ScrollView { LinearGradient( colors: [.red, .blue], startPoint: .top, endPoint: .bottom ) .frame(height: 1000) } .safeAreaInset(edge: .bottom) { Color.green .frame(height: 50) } .ignoresSafeArea(edges: .bottom) } }
调整后绿色视图会完全贴合屏幕底部,同时safeAreaInset原有的特性不会受影响:ScrollView滚动到底部时,内容顶部依然会完整展示在绿色视图上方,不会被遮挡。
内容的提问来源于stack exchange,提问作者George
相关产品推荐
相关产品推荐

