SwiftUI视图内容布局意外弹出/跳转出现? [英] SwiftUI view content layout unexpectedly pop / jumps on appear?

查看:318
本文介绍了SwiftUI视图内容布局意外弹出/跳转出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用表单或列表(分组)导航到新视图时,内容从导航视图标题的底部向下大约四分之一出现,然后弹出"到正确的位置(大约一半)第二秒之后.十分难看.不确定这是SwiftUI错误还是结构错误?

When navigating to a new view with a form, or list (grouped), the content appears about a quarter of the way down from the bottom of the navigation view title, then "pops" up to the correct location about a half second later. Very ugly. Not sure if this is a SwiftUI bug or i'm structuring something incorrectly?

我已经在模拟器和设备上对此进行了测试,结果相同.

I have tested this on both the simulator and device with same results.

我是否认为这与导航链接的处理方式有关?当出现ContentView时,每个导航链接都会在其目标视图上调用init().对我而言,这没有任何意义,仅在用户实际单击导航链接时才调用目标init()吗?

Im thinking it has something to do with the way navigation links are handled? Every navigation link calls the init() on its destination view when ContentView appears. To me this makes no sense, shouldnt the destination init() only be called the moment a user actually clicks the navigation link?

我也尝试过使用工作表进行此操作,并且仅当使用具有按样式分组的表单或列表的导航链接时,工作表才存在此问题.

I have tried this with a sheet as well and the problem does not exist with sheets, only when using navigation links with forms or lists styled as grouped.

来自全新的单视图应用程序的代码:

Code from a brand new single view application:

struct ContentView: View {

    var body: some View {
        NavigationView {
            NavigationLink(destination: TestView()) {
                Text("Test Link")
            }
        .navigationBarTitle("Content View")
        }
    }

}

struct TestView: View {

    var body: some View {
        Form {
            Text("Test View")
        }
        .navigationBarTitle("Test View")
    }

}

推荐答案

为节提供标题文本可以解决此问题,即使该文本为空白.尽管这会在导航标题和第一行之间留出一定的距离.

Providing a section with a header text fixes the issue, even if the text is blank. Although this will leave a gap between the navigation title and first row.

同时使用Forms&样式为分组的列表.

Works with both Forms & Lists styled as grouped.

struct TestView: View {

    var body: some View {
        Form {
            Section(header: Text("")) {
                Text("Test View")
            }
        }
        .nvigationBarTitle("Test View")
    }
}

这篇关于SwiftUI视图内容布局意外弹出/跳转出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆