简体   繁体   English

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

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

I have this variable:我有这个变量:

@State var profileViewHeight: CGFloat = 200

and I need to update it based on a view as soon as it appears我需要在视图出现后立即对其进行更新

This is what I've tried:这是我尝试过的:

    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
    }

and I get the我得到了

Type '()' cannot conform to 'View'

error.错误。

I also tried:我也试过:

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

but the ui gets bugged this way.但是用户界面会这样被窃听。

Please help请帮忙

Ok I got it:D好的,我明白了:D

.background(
    GeometryReader { geo in

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

but it looks like it still creates an ui bug, I'll accept a solution which is better than this one但看起来它仍然会产生一个 ui 错误,我会接受比这个更好的解决方案

What I meant:我的意思是:

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)
}

or或者

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)
}

Try simple first先试试简单

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

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