새소식

iOS/UIKit

[iOS] 기종별로(iPhone, iPad) StoryBoard 구분하기

  • -

 

두 개의 Storyboard 생성합니다.

 

SceneDelegate.swift에서 코드 추가

 

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }
        var initialViewController = UIViewController()
        var storyboard = UIStoryboard()
        
        if UIDevice.current.userInterfaceIdiom == .pad{
            storyboard = UIStoryboard(name: "iPad_Main", bundle: nil)
        }
        else{
            storyboard = UIStoryboard(name: "Main", bundle: nil)
        }
        
        initialViewController = storyboard.instantiateInitialViewController()!
        
        self.window = UIWindow(windowScene: windowScene)
        self.window?.rootViewController  = initialViewController
        self.window?.makeKeyAndVisible()
    }

 

 

결과

좌: iPhone, 우: iPad

 

 


아이패드에서 가로모드로 따로 적용하기

기종 구분을 하는 UIDevice.current.userInterfaceIdiom을 viewDidLayoutSubviews()에 선언하여 구분

    override func viewDidLayoutSubviews() {
        
        if UIDevice.current.userInterfaceIdiom == .pad{
            
            if UIScreen.main.bounds.size.width > UIScreen.main.bounds.size.height{
                // 화면 크기 구하기
                let screenSize = UIScreen.main.bounds.size
                let componentsViewWidth = screenSize.width / 3
                let imgBackgroundWidth = screenSize.width - componentsViewWidth
                
                imgBackground.frame = CGRect(x: 0, y: 0, width: imgBackgroundWidth, height: screenSize.height)
                componentsView.frame = CGRect(x: imgBackgroundWidth, y: 0, width: componentsViewWidth, height: screenSize.height)
                componentsView.frame.size.width = componentsViewWidth
                componentsView.frame.size.height = screenSize.height
                componentsView.backgroundColor = .systemGray3
            }
            else{
                componentsView.backgroundColor = .clear
            }
        }
    }

 

728x90

'iOS > UIKit' 카테고리의 다른 글

[iOS] @IBInspectable  (0) 2024.01.06
[iOS] 키보드 처리  (0) 2023.12.30
[iOS] 한 개의 ViewController에서 여러 개의 tableView 처리 방법  (0) 2023.06.17
[iOS] TableView  (0) 2023.01.24
[iOS] 키보드 내리기  (0) 2023.01.14
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.