热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:SwiftUI中多设备运行方法

https://blog.csdn.net/weixin_42679753/article/details/94465674

https://blog.csdn.net/weixin_42679753/article/details/94465674

https://www.jianshu.com/p/17fc7929fcbb

1.方法一

#if DEBUG

struct LandmarksList_Previews: PreviewProvider {

    static var previews: some View {

        ForEach(["iPhone SE", "iPhone XS Max"].identified(by: .self)) { deviceName in

            LandmarkList()

                .previewDevice(PreviewDevice(rawValue: deviceName))

                .previewDisplayName(deviceName)

              //.previewLayout(.fixed(width: 300, height: 70)) 设置view控件大小

        }

        .environmentObject(UserData())

    }

}

#endif

 

2.方法二

import SwiftUI
struct WatchLandmarkDetail: View {
@EnvironmentObject var userData: UserData
var landmark: Landmark var landmarkIndex: Int {
userData.landmarks.firstIndex(where: { $0.id == landmark.id })!
} var body: some View {
ScrollView {
VStack {
CircleImage(image: self.landmark.image.resizable())
.scaledToFit()

Text(self.landmark.name)
.font(.headline)
.lineLimit(0)

Toggle(isOn:
$userData.landmarks[self.landmarkIndex].isFavorite) {
Text("Favorite")
}

Divider()

Text(self.landmark.park)
.font(.caption)
.bold()
.lineLimit(0)

Text(self.landmark.state)
.font(.caption)
}
.padding(16)
}
//
.navigationBarTitle("Landmarks")
//
}
}
struct WatchLandmarkDetail_Previews: PreviewProvider {
static var previews: some View {
let userData = UserData()
return Group {
WatchLandmarkDetail(landmark: userData.landmarks[0]).environmentObject(userData)
.previewDevice("Apple Watch Series 4 - 44mm")

WatchLandmarkDetail(landmark: userData.landmarks[1]).environmentObject(userData)
.previewDevice("Apple Watch Series 2 - 38mm")
}
}
}

 

 

 

 

 

 


推荐阅读
author-avatar
常叽叽_655
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有