iT邦幫忙

2023 iThome 鐵人賽

DAY 25
0
Mobile Development

SwiftUI 男孩系列 第 25

Day 25: UIHostingController 🫸🏻🤏🏻 

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20231009/20130138gXxzekf25H.jpg
Photo by Tonia Kraakman on Unsplash
Mount Cook, Canterbury, New Zealand

這橋在紐西蘭的Mt.Cook 庫克山,我有走過耶,Hooker Valley Track路段會有經過三座吊橋到達Hooker Lake (胡克湖),冬天來可以看見胡克冰川(Hooker Glacier)🏔️


假如有專案一年以上用 UIKit 開發功能很完整,總不能開新的專案,新的功能不做,全力花半年重新做 SwiftUI 版本。這時用蠶食鯨吞策略慢慢原有 StoryBoard UIKit 改嵌入 SwiftUI 🫸🏻🤏🏻 

MyViewController → MySwiftUIView
Press ‘Command + N’ to create a new Swift file named ‘MySwiftUIView’

import SwiftUI

class MySwiftUIViewHostingController: UIHostingController<MySwiftUIView> {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder, rootView: MySwiftUIView())
    }
}

struct MySwiftUIView: View {
    var body: some View {
        Text("Hello, World!")
    }
}

struct MySwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        MySwiftUIView()
    }
}

把 StoryBoard 的 class 改成 MySwiftUIViewHostingController
https://ithelp.ithome.com.tw/upload/images/20231009/20130138RZjXM6f7MC.png


Refer:

How to use SwiftUI in Storyboard using UIHostingController subclass | Sarunw


上一篇
Day 24: @ViewBuilder
下一篇
Day 26: UIViewControllerRepresentable
系列文
SwiftUI 男孩30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言