【问题标题】:UIView does not update after updateUIView is called on UIViewRepresentable在 UIViewRepresentable 上调用 updateUIView 后 UIView 不更新
【发布时间】:2022-01-09 13:09:05
【问题描述】:

updateUIView 函数中更改其属性后,我的 UIView 没有更新。为了测试它,我点击 VStack 中调用generateBarcodeData 的按钮并更改条形码的状态。

我在BarCodeView 中监视了updateUIView 函数,它确实被调用了,但是我在模拟器上看不到任何变化。

import SwiftUI

struct MainView: View {
    let screenSize = UIScreen.main.bounds
    let titleOffset = UIScreen.main.bounds.height/25
    let mainModalOffset = UIScreen.main.bounds.height/10
    
    @State private var barcode: String = "&723852390"

    var body: some View {
        ZStack() {
            Color.blue.ignoresSafeArea()
            VStack() {
                Text("-|||||-")
                    .font(.system(.title, design: .rounded))
                    .fontWeight(.semibold)
                    .foregroundColor(Color.yellow)
                    .offset(y: titleOffset)
                Spacer()
            }
            VStack() {
                BarCodeView(barcode: $barcode)
                    .frame(height: screenSize.height/2.5)
                    .padding()
                Button(action: {
                    generateBarcodeData()
                })
                {
                    Text("Reset Barcode")
                        .font(.headline)
                        .foregroundColor(.white)
                        .padding()
                        .frame(maxWidth: .infinity)
                        .background(Color.blue)
                        .cornerRadius(10.0)
                        .padding(.bottom, 20)
                }
            }
            .padding()
            .padding(.bottom, 150)
            .frame(height: screenSize.height)
            .background(Color.white)
            .offset(y: mainModalOffset)
        }
    }
    func generateBarcodeData() {
//        let src128API = Src128API(username: self.username, password: self.password)
//        src128API.getBarcode() { (barcodeData) in
//            barcode = barcodeData
//            print(barcodeData)
//        }
        let min: UInt32 = 100_000_000
        let max: UInt32 = 999_999_999
        let i = min + arc4random_uniform(max - min + 1)
        barcode = String(i)
        print(barcode)
    }
}
extension UIImage {

    convenience init?(barcode: String) {
        let data = barcode.data(using: .ascii)
        guard let filter = CIFilter(name: "CICode128BarcodeGenerator") else {
            return nil
        }
        filter.setValue(data, forKey: "inputMessage")
        guard let ciImage = filter.outputImage else {
            return nil
        }
        self.init(ciImage: ciImage)
    }

}

struct BarCodeView: UIViewRepresentable {
    @Binding var barcode: String
    func makeUIView(context: Context) -> UIImageView {
        let imageView = UIImageView()
        return imageView
    }

    func updateUIView(_ uiView: UIImageView, context: Context) {
        uiView.image = UIImage(barcode: barcode)
    }
}

struct MainView_Previews: PreviewProvider {
    static var previews: some View {
        MainView()
    }
}

【问题讨论】:

  • 在 Xcode 12.5.1 中工作正常,可能数据总是生成相同的字符串,因此条形码图像不会更新。
  • 也适用于我,在 macos 12.1-beta 上,使用 xcode 13.2-beta。 (为了测试我在generateBarcodeData中使用了barcode = UUID().uuidString
  • 自从我记录了barcode 的值以来,它每次都会生成一个新的条形码,并且每次都会改变。不知道这是不是等待API完成的问题?我已经编辑了上面的代码以匹配我所拥有的。
  • 随意将新代码复制粘贴到 xcode 中并进行测试。由于某种原因,我无法让它工作。

标签: ios swift swiftui uiview uiimage


【解决方案1】:

我在我的 iPhone 上构建了代码,它在那里工作。我猜它只是在模拟器上坏了。

【讨论】:

    猜你喜欢
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    相关资源
    最近更新 更多