2

I have a sheet that is presented when a button is pressed:

struct Button: View {
    @State isPresented = false
    var body: some View {
        Button(action: { self.isPresented.toggle() },
        label: { Text("Text Label") }
        ).sheet(isPresented: $isPresented, content: { 
            //sheet contents in here
        })
    }
}

The class seen below is essentially the view in UIKit (I removed some of the code from the functions because it doesn't really have anything to do with the problem, but I kept the function names in there with descriptions so you can interpret what it's doing)

class CustomCalloutView: UIView, MGLCalloutView {

lazy var leftAccessoryView = UIView()
lazy var rightAccessoryView = UIView()

weak var delegate: MGLCalloutViewDelegate?

//MARK: Subviews -

let personImg: UIImageView = {
    let img = UIImage(systemName: "person.fill")
    var imgView = UIImageView(image: img)
    //imgView.tintColor = UIColor(ciColor: .black)
    imgView.translatesAutoresizingMaskIntoConstraints = false
    return imgView
}()

let clockImg: UIImageView = {
    let img = UIImage(systemName: "clock")
    var imgView = UIImageView(image: img)
    //imgView.tintColor = UIColor(ciColor: .black)
    imgView.translatesAutoresizingMaskIntoConstraints = false
    return imgView
}()


//Initialization of the view
required init() {
    super.init(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: UIScreen.main.bounds.width * 0.75, height: 130)))
    setup()
}
//other initializer
required init?(coder decoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
//essentially just positioning the view
override var center: CGPoint {
        set {
            var newCenter = newValue
            newCenter.y -= bounds.midY
            super.center = newCenter
        }
        get {
            return super.center
        }
    }
//setting it up
func setup() {
    // setup this view's properties
    self.backgroundColor = UIColor.clear
    self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(CustomCalloutView.calloutTapped)))

    // And the subviews
    self.addSubview(personImg)
    self.addSubview(clockImg)

    // Add Constraints to subviews

    //Positioning the clock image
    clockImg.topAnchor.constraint(equalTo: self.separatorLine.bottomAnchor, constant: spacing).isActive = true
    clockImg.leftAnchor.constraint(equalTo: self.leftAnchor, constant: spacing).isActive = true
    clockImg.rightAnchor.constraint(equalTo: self.timeLabel.leftAnchor, constant: -spacing / 2).isActive = true
    clockImg.heightAnchor.constraint(equalToConstant: 20.0).isActive = true

    //Positioning the person image
    personImg.topAnchor.constraint(equalTo: self.timeLabel.bottomAnchor, constant: spacing / 2).isActive = true
    personImg.leftAnchor.constraint(equalTo: self.leftAnchor, constant: spacing).isActive = true
    personImg.rightAnchor.constraint(equalTo: self.peopleLabel.leftAnchor, constant: -spacing / 2).isActive = true
    personImg.heightAnchor.constraint(equalToConstant: 20.0).isActive = true

}



func presentCallout(from rect: CGRect, in view: UIView, constrainedTo constrainedRect: CGRect, animated: Bool) 
//presenting the view

}

func dismissCallout(animated: Bool) {
    //dismissing the view

}

@objc func calloutTapped() {
    //respond to the view being tapped
}

}

When the sheet is presented and then dismissed, it causes other UI elements that are coded in UIKit (like text buttons, for example) to have their text turn color to the systemBlue UIColor...any idea how to fix this?

4
  • Try .foregroundColor(.black). Button labels are blue by default. Commented Jun 1, 2020 at 22:29
  • @pawello2222 The UI elements that are changing are part of a view that is composed entirely of UIKit though, and foregroundColor isn't a property of UIKit, right? Commented Jun 1, 2020 at 22:34
  • You didn't mention anything about UIKit. Maybe update your question so we can see these other elements. Commented Jun 1, 2020 at 22:36
  • @pawello2222 I updated it, the personImg and clockImg are the UI elements that change to a blue color after the sheet is presented and then dismissed... Commented Jun 2, 2020 at 15:55

1 Answer 1

1

There is not enough code provided to test, but the reason might be in

struct Button: View { // << this custom view named as standard Button !!!
    @State isPresented = false

it is named the same as standard SwiftUI component Button so this can confuse rendering engine.

Try to rename this (and others if you practice this) to something unique to your app, like MyButton or CustomButton, SheetButton, etc.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I tried to keep it very generic so that this answer could help people in the future. The UI elements that are changing are personImg and clockImg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.