由于某种原因,我的UIImagePickerController
有一个白色的Cancel
按钮,很难看到...
这是我的ImagePicker
班:
struct ImagePicker: UIViewControllerRepresentable {
let key: String
@Binding var uiImage: UIImage?
@Environment(\.presentationMode) var presentationMode
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIViewController(context: Context) -> UIImagePickerController {
let controller = UIImagePickerController()
controller.navigationBar.topItem?.rightBarButtonItem?.tintColor = UIColor.orange
controller.navigationBar.topItem?.leftBarButtonItem?.tintColor = UIColor.orange
controller.navigationBar.backItem?.leftBarButtonItem?.tintColor = UIColor.orange
controller.sourceType = .photoLibrary
controller.delegate = context.coordinator
return controller
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {
}
class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
var parent: ImagePicker
init(_ parent: ImagePicker) {
self.parent = parent
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let img = info[.originalImage] as? UIImage {
parent.uiImage = img
}
parent.presentationMode.wrappedValue.dismiss()
}
}
}
struct Dialog: View {
@Binding var dialogKey: String?
@Binding var showImagePicker: Bool
@Binding var changed: Bool
@Binding var uiImage: UIImage?
@Binding var loadingKey: String?
@State var noImage = false
var body: some View {
HStack(spacing: 0) {
Button(action: { self.showImagePicker.toggle() }){
Text(noImage ? "Add" : "Replace")
.frame(width: 80)
.multilineTextAlignment(.center)
.font(.custom("Seravek-Bold", size: 15))
.foregroundColor(Color.white)
.padding(.vertical, 15)
.background(
Rectangle()
.fill(Color("Meeq"))
)
.cornerRadius(radius: 5, corners: [.topLeft, .bottomLeft])
.shadow(color: Color.black.opacity(0.5), radius: 1, x: 0, y: 1)
}
Button(action: { self.deleteImage() }){
Text("Delete")
.frame(width: 80)
.multilineTextAlignment(.center)
.font(.custom("Seravek-Bold", size: 15))
.padding(.vertical, 15)
.foregroundColor(Color.white)
.background(
Rectangle()
.fill(Color("Exit"))
)
.shadow(color: Color.black.opacity(0.5), radius: 1, x: 0, y: 1)
.opacity(noImage ? 0.3 : 1.0)
}
.disabled(noImage)
.onAppear {
// Check if there's no image for the selected circle
if let dialogKey = dialogKey {
let key = UserDefaults.standard.string(forKey: dialogKey)
if key == nil { noImage = true }
print("Delete onAppear: \(key)")
}
}
Button(action: { self.dialogKey = nil }){
Text("Cancel")
.frame(width: 80)
.multilineTextAlignment(.center)
.font(.custom("Seravek-Bold", size: 15))
.foregroundColor(Color.white)
.padding(.vertical, 15)
.background(
Rectangle()
.fill(Color.gray)
)
.cornerRadius(radius: 5, corners: [.topRight, .bottomRight])
.shadow(color: Color.black.opacity(0.5), radius: 1, x: 1, y: 1)
}
}
.sheet(isPresented: $showImagePicker, onDismiss: loadImage){
if let key = dialogKey {
ImagePicker(key: key, uiImage: self.$uiImage)
.accentColor(Color.orange)
}
}
}
}
如您所见,我尝试使用以上代码将“取消”按钮的颜色更改为橙色:
controller.navigationBar.topItem?.rightBarButtonItem?.tintColor = UIColor.orange
controller.navigationBar.topItem?.leftBarButtonItem?.tintColor = UIColor.orange
controller.navigationBar.backItem?.leftBarButtonItem?.tintColor = UIColor.orange
但是它仍然是白色的。
您知道如何将Cancel
按钮变成橙色吗?