微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift ui 小部件占用太多空间的项目列表

如何解决Swift ui 小部件占用太多空间的项目列表

enter image description here

正如您从图像中看到的,我有一个包含一系列元素的小部件,我必须在元素系列的末尾放置单词 Next update,它必须在小部件的末尾找到一些.

问题是:

  • 如果元素很少,则 Next update 太高。
  • 如果有很多元素,则不会显示 Next update 消息。

你能帮我一把吗?

附言

如果你认为它应该写成不同的,请告诉我,提前谢谢你。

struct GitCommitWidgetEntryView : View {
    var entry: ProviderCommit.Entry
    @Environment(\.colorScheme) var colorScheme
    let firstColor: UInt = getDate() ? 0x4688B4 : 0x030721
    
    var body: some View {
        GeometryReader { geometry in
            if(entry.loading){
                if(!entry.error){
                    vstack() {
                        if(entry.user != ""){
                            HStack {
                                Spacer()
                                Text("\(entry.user) (\(entry.commits.count))")
                                    .font(.caption)
                                    .foregroundColor(Color.black)
                                    .shadow(
                                        color: Color.black,radius: 1.0,x: CGFloat(1),y: CGFloat(1)
                                    )
                                Spacer()
                            }
                            .background(Color(hex: 0xff9800))
                        }
                        if(entry.commits.count > 0){
                            ForEach(entry.commits,id:\.id){
                                item in
                                Text(item.commit.message)
                                    .shadow(
                                        color: Color.black,y: CGFloat(1)
                                    )
                                Text("\(item.author.login) \(convertDate(date: item.commit.author.date))")
                                    .shadow(
                                        color: Color.black,y: CGFloat(1)
                                    )
                                Divider()
                            }
                            .frame(alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                        }else{
                            Text("There are no repositories.")
                                .font(.caption)
                                .foregroundColor(Color.black)
                                .padding(2)
                                .background(Color.white.opacity(0.4))
                                .cornerRadius(5)
                                .padding(.bottom,3)
                                .padding(.horizontal,/*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/)
                                .multilineTextAlignment(.center)
                                .frame(maxWidth: .infinity,maxHeight: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/,alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                        }
                        HStack {
                            Spacer()
                            Text("Next update")
                                .font(.caption)
                                .foregroundColor(Color.black)
                                .multilineTextAlignment(.center)
                                .shadow(
                                    color: Color.black,y: CGFloat(1)
                                )
                            Spacer()
                        }
                        .background(Color(hex: 0xff9800))
                    }
                    .background(LinearGradient(
                                    gradient: Gradient(colors: [
                                        Color(hex: firstColor),Color(hex: 0xffffff)
                                    ]),startPoint: .top,endPoint: .bottom)
                    )
                }else{
                    vstack() {
                        Text("No user exist.")
                            .font(.caption)
                            .foregroundColor(Color.black)
                            .padding(2)
                            .background(Color.white.opacity(0.4))
                            .cornerRadius(5)
                            .padding(.bottom,3)
                            .padding(.horizontal,/*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/)
                            .multilineTextAlignment(.center)
                    }
                    .frame(maxWidth: .infinity,alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                    .background(Color.red)
                }
            }else{
                vstack() {
                    Text("No user selected.")
                        .font(.caption)
                        .foregroundColor(Color.black)
                        .padding(2)
                        .background(Color.white.opacity(0.4))
                        .cornerRadius(5)
                        .padding(.bottom,3)
                        .padding(.horizontal,/*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/)
                        .multilineTextAlignment(.center)
                }
                .frame(maxWidth: .infinity,alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                .background(Color.green)
            }
        }
    }
}

解决方法

您需要的方法是将“标签始终在屏幕上”和“增长列表”分开,示意性地应该像

VStack {
  Text("Header label")
  Spacer()
  Text("Footer label")
}
.background(
   VStack {
     Text("Header label").opacity(0)     // for content offset
                                    // or constant height spacer
     ForEach ... // list here
   }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。