How To Separate UI For Each Device

YongJunCha·2022년 1월 28일
0

swift

목록 보기
15/18
post-thumbnail
/// Mark: - UIDevice Extension
/// Feature: 디바이스별 UI 및 로직 대응을 위한 extension
/// HowToUse: UIDevice.current. 뒤에 아래의 메소드명을 적으면 됨.
extension UIDevice {
    public var isiPhoneSE: Bool {
        if UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.phone && (UIScreen.main.bounds.size.height == 568 || UIScreen.main.bounds.size.width == 320) {
            return true
        }
        return false
    }
    
    // iphone 7,8
    public var isiPhoneSE2: Bool {
        if UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.phone && (UIScreen.main.bounds.size.height == 667 || UIScreen.main.bounds.size.width == 375) {
            return true
        }
        return false
    }
    
    public var isiPhonePlus: Bool {
        if UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.phone && (UIScreen.main.bounds.size.height == 736 || UIScreen.main.bounds.size.width == 414) {
            return true
        }
        return false
    }

    public var isiPad: Bool {
        if UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad && (UIScreen.main.bounds.size.height == 1024 || UIScreen.main.bounds.size.width == 768) {
            return true
        }
        return false
    }
    public var isiPadPro12: Bool {
        if UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad && (UIScreen.main.bounds.size.height == 1366 || UIScreen.main.bounds.size.width == 1366) {
            return true
        }
        return false
    }
}

SwiftUI에서 가끔 ViewControllerRepresentable을 프로토콜을 준수하다 보면
얘기치 않게 ZStack으로 해결해야 하는 경우가 생긱는데 이럴 경우에는 아주 문제가 복잡해진다.

이번의 경우에는 보안키패드를 적용하면서 보안 키패드 솔루션이 UIKit용으로 나온 것이었고,
이것을 SwiftUI로 Wrapping해서 쓰다보니 투명한 판처럼 보안 키패드만 눌리고, 다른 UIButton이
눌리지 않게 되었다.

이럴 경우 패딩을 잘 쓸 수 없는데 어쩔 수 없이 상기의 UIDevice.current를 조건문으로 줘서
iPhone SE, SE2, 7, 8 과 같이 화면이 작은 Device에는 개별 대응을 해줘야한다.

SwiftUI에 맞는 라이브러리들이 하루빨리 많이 나왔으면 좋겠다.

0개의 댓글