SwiftUI中多设备运行方法

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")
        }
    }
}

 

 

 

 

 

 

posted @ 2019-12-02 17:56  sundayswift  阅读(627)  评论(0编辑  收藏  举报