繁体   English   中英

iOS SwiftUI - 根据出现的视图高度更新变量

[英]iOS SwiftUI - update variable based on view height on appear

我有这个变量:

@State var profileViewHeight: CGFloat = 200

我需要在视图出现后立即对其进行更新

这是我尝试过的:

    GeometryReader { geo in
        userProfileView(uid: uid, userid: self.$userid, offsetMenu: self.$offsetMenu, closeThisView: closeThisView)
            .frame(width: UIScreen.main.bounds.width)
            .background(Color.teal)
            .padding(.bottom, profileViewOffset)
            .offset(y: profileViewOffset)
            .zIndex(2)
        
        profileViewHeight = geo.size.height
    }

我得到了

Type '()' cannot conform to 'View'

错误。

我也试过:

    .onAppear(){
        profileViewHeight = geo.size.height
    }

但是用户界面会这样被窃听。

请帮忙

好的,我明白了:D

.background(
    GeometryReader { geo in

        Color.clear
            .onAppear {
                profileViewHeight = geo.size.height
            }
    }
)

但看起来它仍然会产生一个 ui 错误,我会接受比这个更好的解决方案

我的意思是:

GeometryReader { geo in
    userProfileView(uid: uid, userid: self.$userid, offsetMenu: self.$offsetMenu, closeThisView: closeThisView)
        .frame(width: UIScreen.main.bounds.width)
        .background(Color.teal)
        .padding(.bottom, geo.size.height)
        .offset(y: geo.size.height)
        .zIndex(2)
}

或者

GeometryReader { geo in
    let profileViewHeight = geo.size.height
    userProfileView(uid: uid, userid: self.$userid, offsetMenu: self.$offsetMenu, closeThisView: closeThisView)
        .frame(width: UIScreen.main.bounds.width)
        .background(Color.teal)
        .padding(.bottom, profileViewOffset)
        .offset(y: profileViewHeight)
        .zIndex(2)
}

先试试简单

暂无
暂无

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

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