簡體   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