view.traitCollection.horizontalSizeClass returning undefined (0) in viewDidLoad

前端 未结 5 576
忘掉有多难
忘掉有多难 2021-02-12 13:19

I\'m using a UITextView inside a UIPageViewController, and I want to determine the font size based on the size class of the device.

The first slide of the page view is l

5条回答
  •  野的像风
    2021-02-12 13:51

    I found that if I use the main screen's traitCollection, instead of the current view, I get the correct size class:

    if (UIScreen.main.traitCollection.horizontalSizeClass == .compact) {
        fontSize = "20"
    } else {
        fontSize = "28"
    }
    

提交回复
热议问题