在 UIViewRepresentable 上调用 updateUIView 后 UIView 不更新
UIView does not update after updateUIView is called on UIViewRepresentable
我在 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()
}
}
我为我的 iPhone 构建了代码,它在那里运行。我猜它只是在模拟器上坏了。
我在 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()
}
}
我为我的 iPhone 构建了代码,它在那里运行。我猜它只是在模拟器上坏了。