Search code examples
iosuiviewuiviewcontrolleruikituicontainerview

How to switch between Container Views but keeping both 'alive' without using is "isHidden"?


Assuming one has two active container views, how can one best swap between them such there is no flicker?

More detail:

  • I have a calendar & map with segemented control button. So you can click and swap between either the calendar or the map
  • when swapping between the two don't want to have to redraw things so want to keep them "alive" so to speak
  • I have done this is "isHidden" as the approach, however the issue here is things like timing issues when relaying out when the view is hidden, i.e. the hidden view may not have itself picked up its new layout after rotation and the likes. Also there is some flicker.

Would best approach be to "turn off" use of autolayout and position the non-active view off the screen, i.e. so leaving it visible?


Solution

  • You can animate the alpha values of the views:

    UIView.animate(withDuration: 0.7, animations: {
                viewToDisappear.alpha           = 0
                viewToAppear.alpha              = 1
            })
    

    Set duration value to taste.