3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Swift でSize Class を取得する

Last updated at Posted at 2018-01-05

Size Classの値を取得

Overview

Interface Builderのストーリーボードは、デフォルトでサイズクラスを使用します。サイズクラスは、シーンやビューなどのユーザーインターフェイス要素に割り当てられた特性です。それらは要素のサイズの大まかな表示を提供します。Interface Builderを使用すると、現在のサイズクラスに基づいてレイアウトの多くの機能をカスタマイズできます

こんな感じのものをStoryboardで端末毎で指定できます。

下記が端末毎でのSize Class の値になります。

Device Portrait Landscape
iPad (all)
iPad Mini
Vertical size class: Regular
Horizontal size class: Regular
Vertical size class: Regular
Horizontal size class: Regular
iPhone 6 Plus Vertical size class: Regular
Horizontal size class: Compact
Vertical size class: Compact
Horizontal size class: Regular
iPhone 6 Vertical size class: Regular
Horizontal size class: Compact
Vertical size class: Compact
Horizontal size class: Compact
iPhone 5s
iPhone 5c
iPhone 5
Vertical size class: Regular
Horizontal size class: Compact
Vertical size class: Compact
Horizontal size class: Compact
iPhone 4s Vertical size class: Regular
Horizontal size class: Compact
Vertical size class: Compact
Horizontal size class: Compact

IMPORTANT

Never assume that your app will be displayed with a specific size class on a device. Always check the size class found in an object’s trait collection when making decisions about how to configure that object.

Creating Custom Presentations

Building an Adaptive Interface

Copyright © 2017 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2015-09-16

こんな感じのものをStoryboardで指定したりできますが、これをコードで取得します。


        let horizonSizeClass = UITraitCollection(horizontalSizeClass: .regular)
    
        if traitCollection.containsTraits(in: horizonSizeClass) {
            print ("Regular")
        } else {
            print ("Compact")
        }

Apple API UITraitCollection
https://developer.apple.com/documentation/uikit/uitraitcollection

こんな感じで取得できます。

3
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?