Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

swiftui - View that works in iPhone does not work in iPad

I've tried to recreate this problem and just present a nice little code snippet you guys can see and analyze, but I cannot recreate this problem because obviously I'm not understanding something fundamental here.

I have a conditional ZStack view that shows fine in iPhone simulator but will not show in iPad.

This is how it works in iPhone:

text

But I cannot get the same functionality for iPad. Instead I get this error message when I toggle the condition:

[LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x600003c068f0 UIView:0x156ee2890.width == - 16   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600003c068f0 UIView:0x156ee2890.width == - 16   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2021-01-24 12:39:23.586493-0500 MapAppRelease[12010:598172] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x600003c10550 h=--& v=--& UIView:0x157a71da0.height == 13   (active)>",
    "<NSLayoutConstraint:0x600003c017c0 UIView:0x156ee2890.height >= 44   (active)>",
    "<NSLayoutConstraint:0x600003c00fa0 _UIAlertControllerView:0x156ee22b0'Your coordinates are: 
La...'.height == UIView:0x156ee2890.height   (active)>",
    "<NSLayoutConstraint:0x600003c17e80 UIView:0x157a71da0.height == _UIAlertControllerView:0x156ee22b0'Your coordinates are: 
La...'.height   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600003c017c0 UIView:0x156ee2890.height >= 44   (active)>

So I made the symbolic breakpoint @ UIViewAlertForUnsatisfiableConstraints like Xcode suggested. Nothing.

Below is the code to recreate this problem (Again sorry I could not compress the code)

import SwiftUI
import MapKit

struct ContentView: View {
    @State private var centerCoordinates = CLLocationCoordinate2D()
    @State private var showingConfirmation = false
    @State private var showingAlert = false
    var body: some View {
        VStack {
            GeometryReader { geo in
                ZStack {
                    MapView(centerCoordinate: $centerCoordinates)
                        .edgesIgnoringSafeArea(.all)
                        .blur(radius: showingConfirmation ? 6.0 : 0)
                    Circle()
                        .fill(Color.blue)
                        .opacity(0.3)
                        .frame(width: 32, height: 32)
                        .blur(radius: showingConfirmation ? 6.0 : 0)
                    Text("Get Coordinates").bold()
                        .foregroundColor(.primary)
                        .padding()
                        .background(Color.blue)
                        .clipShape(Capsule())
                        .blur(radius: showingConfirmation ? 6.0 : 0)
                        .offset(x: 0, y: geo.size.height * 0.45)
                        .onTapGesture {
                            print(centerCoordinates)
                            showingAlert = true
                        }.disabled(showingConfirmation)

                    if showingConfirmation {
                        Rectangle()
                            .fill(Color.clear)
                            .frame(height: 500)
                            .overlay(
                                
                                VStack {
                                    Text("Latitude and Longitude copied!")
                                        .font(.title)
                                        .bold()
                                        .lineLimit(3)
                                    Button(action: {self.showingConfirmation = false}){
                                        Text("Ok")
                                            .fontWeight(.bold)
                                            .padding()
                                            .foregroundColor(.white)
                                            .background(Color.blue)
                                            .cornerRadius(10)
                                    }

                                }
                            )
                    }
                }
            }
        }
        .actionSheet(isPresented: $showingAlert) {
            ActionSheet(title: Text("Your coordinates are: 
Latitude: (centerCoordinates.latitude) 
Longitude: (centerCoordinates.longitude)"), message: nil, buttons: [
                .default(Text("Ok")),
                .default(Text("Copy Latitude and Longitude")) {
                    let myCoordinates = [String(centerCoordinates.latitude), String(centerCoordinates.longitude)]
                    let pasteboard = UIPasteboard.general
                    pasteboard.strings = myCoordinates
                    showingConfirmation = true
                }
            ]
            )
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .preferredColorScheme(.dark)
    }
}

struct MapView: UIViewRepresentable {
    
    @Binding var centerCoordinate: CLLocationCoordinate2D
    
    func makeUIView(context: Context) -> MKMapView {
        let mapView = MKMapView()
        mapView.delegate = context.coordinator
        return mapView
    }
    
    func updateUIView(_ uiView: MKMapView, context: Context) {
        
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    class Coordinator: NSObject, MKMapViewDelegate {
        var parent: MapView
        
        init(_ parent: MapView) {
            self.parent = parent
        }
        
        func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {
            parent.centerCoordinate = mapView.centerCoordinate
        }
    }
    
    
    
    
   
}

struct MapView_Previews: PreviewProvider {
    static var previews: some View {
        MapView(centerCoordinate: .constant(MKPointAnnotation.example.coordinate))
    }
}

extension MKPointAnnotation {
    static var example: MKPointAnnotation {
        let annotation = MKPointAnnotation()
        annotation.title = "Current Stay"
        annotation.subtitle = "Temporary Maryland Abode"
        annotation.coordinate = CLLocationCoordinate2D(latitude: 20, longitude: -70)
        
        return annotation
    }
}

Please let me know if any other code is needed. That should be it

**This is also my first time using UIPasteboard.general and I believe its causing Xcode to act weird.

question from:https://stackoverflow.com/questions/65874238/view-that-works-in-iphone-does-not-work-in-ipad

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...