0

I actually use this to switch from a ViewController to another

    let vue = MAINSTORYBOARD.instantiateViewController(withIdentifier: "addhoraire") as! Addhoraire
    self.present(vue, animated: true, completion: nil)

sometimes I want to change something before show the view to user so in the VC with will be present I do a func. like that

// header and cercle.exemple are outlets of a UIView
    public func setup(selectedDay : String){
        loadViewIfNeeded()
        self.selectedDay = selectedDay
        print("jour selectionné : " + self.selectedDay)
        self.header.backgroundColor = UIColor.brown
        self.profTextField.backgroundColor = UIColor.yellow
        self.cercleExemple.backgroundColor = UIColor.yellow
    }

And when I want to switch now I do :

    let vue = MAINSTORYBOARD.instantiateViewController(withIdentifier: "addhoraire") as! Addhoraire
    vue.setup(selectedDay: selectedDayinHoraire)
    self.present(vue, animated: true, completion: nil)

So it will switch the new VC and execute setup like I want, ,textfield color is changed, cercleExemple too but header will not. view.backgroundColor, same problem ... but if I do in the same place header.isHidden = true it work. So it just refuse to change color

header and view bgcolor only change in ViewDidAppear. I don't understand why. It's been a week since I've been on this bug, it make me crazy ...

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Cydiaddict
  • 277
  • 4
  • 17
  • I looked at your project, but it is really hard to follow what is going on. What I could determine was that the header's color is changing between `viewWillAppear` and `viewDidAppear`, but I am not sure why. – Dennis W. Jan 31 '19 at 00:04

3 Answers3

0

You are trying to setup a view before it has loaded, save the information you need to set in a variable and set in setup, and when viewDidLoad gets called, set the information from those variables.

Adrian
  • 79
  • 6
  • why outlets work for other views and textfield and not for these ? That's discursive. You want me to in viewDid load { var headerVar = header } and use headerVar in setup ? – Cydiaddict Jan 30 '19 at 16:57
0

As Adrian mentioned, you'll need to wait until the view loads before making changes to it. Have you tried overriding viewDidLoad() in your view controller?

override func viewDidLoad() {
    super.viewDidLoad()

    // Do stuff here
    self.header.backgroundColor = UIColor.brown
    // ...additional view configuration
}

If you can't do this in viewDidLoad(), you could also try making your UI changes in the viewWillAppear() method, instead of waiting until viewDidAppear().

Josh Buhler
  • 26,878
  • 9
  • 29
  • 45
0

I found the issue, but I have no idea of the root cause. This seems like it might be an Apple bug. The issue is occurring because in Storyboard you are setting the header's background color using a color from the Assets.xcassets catalog (LightSideMenu). If instead of choosing a color from the Assets.xcassets you manually set the background color of the header using the RGB sliders or by entering the hex value #2A6D9E, then your header's background color will not be overridden. Also, make sure you are not resetting it elsewhere like in viewWillAppear or viewDidAppear as these will be called after your func setup(selectedDay : String) method.

enter image description here

Dennis W.
  • 646
  • 6
  • 7
  • 1
    Perhaps if you update your question with the entire implementation of the view controller where you have `public func setup(selectedDay : String)` we might be able to offer assistance. – Dennis W. Jan 30 '19 at 22:31
  • Found it, I am going to need a minute or so to look at your code. – Dennis W. Jan 30 '19 at 23:04
  • Dennis's solution work thx ! Do you think that's a Xcode bug or just badly thought feature ? – Cydiaddict Feb 04 '19 at 22:56
  • @CédricLoneux I feel like its a bug. I have never seen something like that before, but I have also not used the assets catalog to store the apps colors either. – Dennis W. Feb 04 '19 at 23:09