3

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 ?

cmii
  • 3,556
  • 8
  • 38
  • 69
  • I originally posted an answer that said outlets to view that aren't visible in the current size class will be `nil`, but I'm completely wrong. I just wrote a test app with a view that contains 9 UILabels, and assigns each one to be installed only for a specific size class. At runtime, I print out which outlets are non-nil, and no matter what the size class is, none of them are ever nil. – NRitH 40 secs ago edit – NRitH Jan 15 '15 at 15:01

2 Answers2

5

To follow up on my now-deleted previous answer and subsequent comment, Apple's docs state that

A runtime object for an uninstalled view is still created. However, the view and any related constraints are not added to the view hierarchy and the view has a superview property of nil. This is different from being hidden. A hidden view is in the view hierarchy along as are any related constraints.

In other words, the only way to know is to check whether the outlet view's parent view is nil.

NRitH
  • 13,441
  • 4
  • 41
  • 44
4

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

myViewController: UIViewController{

    mybutton = UIButton(frame: myframe)

    func viewWillAppear(){

        if traitCollection.horizontalSizeClass == .Compact {
            applyAwesomeEffect(myButton)
        }
    }
}
JMFR
  • 799
  • 5
  • 18
  • It works thanks ! I have just some difficult to distinguish iPad in portait and iPad in landscape using size classes. – cmii Jan 15 '15 at 19:22
  • The iPads are "regular height" and "regular width" in both orientations. – JMFR Jan 15 '15 at 19:24
  • Indeed I know. It's a different problem, someone has already asked the question : http://stackoverflow.com/questions/27700440/ipad-landscape-and-portrait-different-layouts-with-size-class#comment44298907_27700440, but nobody answered. – cmii Jan 15 '15 at 20:06