2

I have a view that on an iPhone I want to take up the width of the entire screen. Which can easily be done with the code below. But, on an iPad I want the view's width to remain phone-sized vs expand to the edge of the device. Does anybody know how to do that?

struct ContentView: View{
   var body: some View{
       HStack{
          Text("Hello World")
       }
       .frame(minWidth:0,maxWidth: .infinity)
       .background(Color.red)
   }
} 

EDIT: If anybody else finds this adding this line let me specify a size specifically for the iPad.

.frame(minWidth:0,maxWidth: UIDevice.current.userInterfaceIdiom == .pad ? 500 : .infinity)

1 Answer 1

2

set the frame depending of the device

switch UIDevice.current.userInterfaceIdiom {
    case .phone:
        // It's an iPhone
    case .pad:
        // It's an iPad
    case .unspecified:
        // Uh, oh! What could it be?
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I added an if statement to the frame and it worked perfectly .frame(minWidth:0,maxWidth: UIDevice.current.userInterfaceIdiom == .pad ? 500 : .infinity)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.