Search code examples
iosstoryboardautolayoutios-universal-appsize-classes

How to know the current size classes and if my button is displayed?


I use size classes in my projet.

I have a button, which is displayed only for

  • Compact Width and Compact height
  • Compact width and Regular height

This button isn't displayed for other size classes.

On this button I apply a specific effect. But I need to apply this effect ONLY for my 2 sizes classes.

Something like :

if button is available for the current size classe
    apply the effect

Is it possible to implement ?


Solution

  • During ViewWillApper() check the traitCollection of your View Controller.

    myViewController: UIViewController{
    
        mybutton = UIButton(frame: myframe)
    
        func viewWillAppear(){
    
            if traitCollection.horizontalSizeClass == .Compact {
                applyAwesomeEffect(myButton)
            }
        }
    }