cmii
cmii

Reputation: 3626

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

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 ?

Upvotes: 3

Views: 770

Answers (2)

JMFR
JMFR

Reputation: 809

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

myViewController: UIViewController{

    mybutton = UIButton(frame: myframe)

    func viewWillAppear(){

        if traitCollection.horizontalSizeClass == .Compact {
            applyAwesomeEffect(myButton)
        }
    }
}

Upvotes: 4

NRitH
NRitH

Reputation: 13893

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.

Upvotes: 5

Related Questions