UIHostingController

2024. 10. 12. 21:03·iOS/UIKit
반응형

정의

 

swiftUI view를 관리하는 뷰컨틀롤러이다.

 

구현

swiftUI의 TextFiled의 text를 변경하여 UIViewController의 색을 바꾸는 미션을 해보자.

 

단계는 다음과 같다.

  1. SwiftUIView 구현 
  2. viewController에 UIHostingController와 함께 등록

1. swiftUI TextField 구현

struct CustomSwiftUITextView : View {
    
    @Binding var text: String
    
    var body: some View {
        TextField("SwiftUI TextField", text: $text)
            .padding()
            .background(Color.blue)
    }
}

 

2.  등록

class ViewController: UIViewController {

   private var text: String = "" {
        didSet {
            self.view.backgroundColor = UIColor.random()
        }
    }
    ...
    
   override func viewDidLoad() {
        super.viewDidLoad()

        let swiftUITextField = CustomSwiftUITextView(text: Binding(get: { self.text }, set: { newValue  in
            self.text = newValue
        })

        )

        let hostingController = UIHostingController(rootView: swiftUITextField)
        addChild(hostingController) // 부모에서 자식 등록 ,viewController의 children에 등록
        view.addSubview(hostingController.view) // subview 등록

        hostingController.view.translatesAutoresizingMaskIntoConstraints = false
        hostingController.didMove(toParent: self) // 자식에서 부모를 등록
    }

}

결과

 


출처

 

UIHostingController | Apple Developer Documentation

A UIKit view controller that manages a SwiftUI view hierarchy.

developer.apple.com

 

 

SwiftUI와 UIKit 같이 사용하기 | Hohyeon Moon

SwiftUI와 UIKit을 같이 사용하는 방법에 대해 알아봅니다

www.hohyeonmoon.com

 

반응형

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

iOS Cache  (7) 2024.10.17
키보드 반응하기  (1) 2024.10.13
UIEditMenuInteraction  (3) 2024.10.12
UISearchController  (1) 2024.10.06
NSKeyedArchiver  (0) 2024.09.11
'iOS/UIKit' 카테고리의 다른 글
  • iOS Cache
  • 키보드 반응하기
  • UIEditMenuInteraction
  • UISearchController
Hamp
Hamp
남들에게 보여주기 부끄러운 잡다한 글을 적어 나가는 자칭 기술 블로그입니다.
  • Hamp
    Hamp의 분리수거함
    Hamp
  • 전체
    오늘
    어제
    • 분류 전체보기 (311) N
      • CS (30)
        • 객체지향 (2)
        • Network (7)
        • OS (6)
        • 자료구조 (1)
        • LiveStreaming (3)
        • 이미지 (1)
        • 잡다한 질문 정리 (0)
        • Hardware (2)
        • 이론 (6)
        • 컴퓨터 그래픽스 (0)
      • Firebase (3)
      • Programing Langauge (39) N
        • swift (32)
        • python (6) N
        • Kotlin (1)
      • iOS (132)
        • UIKit (37)
        • Combine (1)
        • SwiftUI (32)
        • Framework (7)
        • Swift Concurrency (22)
        • Tuist (6)
        • Setting (11)
        • Modularization (1)
        • Instruments (6)
      • PS (59)
        • 프로그래머스 (24)
        • 백준 (13)
        • LeetCode (19)
        • 알고리즘 (3)
      • Git (18)
        • 명령어 (4)
        • 이론 (2)
        • hooks (1)
        • config (2)
        • action (7)
      • Shell Script (2)
      • Linux (6)
        • 명령어 (5)
      • Spring (15)
        • 어노테이션 (3)
        • 튜토리얼 (11)
      • CI-CD (4)
      • Android (0)
        • Jetpack Compose (0)
      • AI (3) N
        • 이론 (3) N
  • 블로그 메뉴

    • 홈
    • 태그
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    프로그래머스
    AVFoundation
    boostcamp
    protocol
    Spring
    투포인터
    CS
    IOS
    dfs
    Swift
    백준
    Tuist
    GIT
    SwiftUI
    lifecycle
    property
    dp
    concurrency
    dispatch
    UIKit
  • 최근 댓글

  • 최근 글

  • 반응형
  • hELLO· Designed By정상우.v4.10.0
Hamp
UIHostingController
상단으로

티스토리툴바