Update UI when orientation is changing in SwiftUI - swift

I've started to learn SwiftUI. I try to update UI when orientation is changing.
I added #Environment(\.horizontalSizeClass) var sizeClass in my view, and then iphone simulator updates the UI automatically when rotated. But iPad simulator does not update the UI when rotated. Could you tell me how to update UI for iPad?

If you take a look at the Device Size Classes in the Human Interface Guidelines, you'll notice that the iPads have 'Regular' width and height in both landscape and portrait mode.
So when you use .horizontalSizeClass, it is working on both iPhone and iPad, it's just that the iPad does not change sizeClass when rotated.
https://developer.apple.com/design/human-interface-guidelines/ios/visual-design/adaptivity-and-layout/
If you set frame sizes with a GeometryReader, they will be automatically updated when the device is rotated.
It's also pretty useful to just set the .frame(maxWidth:) on views, so that if the iPad is landscape, the view's content will be limited. Somewhere around 715 works well.

Related

AutoLayout problems in iPhone5

I have a Xcode 4.5.2 Project
I have a story board with with four UIViews.
Autolayout is Set to ON in the StoryBoard
Deployment is iOS 6
I want the letterbox to appear on top and bottom when running on iPhone 5 and not have my views resized when running on a iPhone5
When I run my app in iPhone5 - I do not see the letterBox - the UIViews are automatically stretched (incorrectly)
When I run the App on iPhone 4 - Everything is as expected.
Why does the letterbox not show as I would expect?
If for some reason you want your app to behave on iPhone 5 the way older apps do -- automatically letterboxed by the OS to 3.5-inch screen dimensions, just don't include a Default-568h#2x.png in your app. However, Apple expects new apps submitted to the App Store to support 4-inch screens, so you might not get very far with this strategy if you're expecting to distribute your app that way.
If you want to "support" iPhone 5 (and iPod touch 5th generation) screens, but keep most of your UI at older screen sizes... well, your users may not like it (and the App Store reviewers might not either), but it's possible using Auto Layout. The catch is that the root view in your window's (or rather, your view controller's) view hierarchy is always automatically resized to fit the screen -- but there's no saying that has to be the main container for your actual UI content.
In IB, with the editor in 3.5 inch screen mode, drop a new UIView into your view controller's root view. It should automatically size to fill the screen (460 points high).
Add a Pin > Height constraint so the view stays 460 points high no matter how its superview gets resized.
Add an Align > Vertical Center in Container constraint so the view stays centered when the height of its superview changes.
Put all of your UI inside this view.
Now you'll get white bars (change the color of the root view if you like) above and below your UI when on iPhone 5.
Note this only works as long as your UI doesn't rotate to landscape. I don't think you can do this kind of artificial letterboxing for both orientations using only one set of constraints, so if you want to support rotation you'll need to respond to an orientation change by switching out constraints programmatically -- pin height and center vertically in portrait, pin width and center horizontally in landscape.

Making a UIView landscape

I have created an iPhone app, which is a tab bar application and only in portrait orientation.
From one tab I am presenting a modal view which contain UIImage view.
Can I make landscape orientation only to that view while rotating iPhone to 90 degree?
yes , you can.
There are a few settings, code part which need to make it.
Depends also for which version, hopefully aren't targeting all supported by xcode, because it is horrible. Better choose only one, like the lastest iOS.

How To add single iPad custom view in existing iPhone app running on iPad?

I have created an iOS application targeting iPhone. Its not universal application but running on both iPhone and iPad (user can view in 1x and 2x view mode). Now i want to add one single view(xib) for iPad if someone is using the app on ipad and turn the device to landscape mode, is it possible for them to see this custom view? Just the layout would be altered to take advantage of the screen size ?
If its is possible how will I proceed. I am thinking to add a viewcontroller along with Xib targeted for Ipad. This will be added to currentview when it is rotated to landscape orientation and it will be swiped by parent view when it is rotated to portarit mode.
Can i make use of :
UI_USER_INTERFACE_IDIOM()
Thanks in advance.

is there a declarative approach to have one's background image changed from the portrait to landscape version upon orientation change in IOS?

Background - I have an iPhone/iPad app that has a background image in the main view. I'd like to introduce a specific portrait and landscape image for this background image, which is in a UIImageView subview.
Question - is there a declarative approach to have one's background image changed from the portrait to landscape version upon orientation change in IOS? For example through some file name convention approach?
(i.e. or do you have to manually change the image yourself each orientation change in the view controller's didRotateFromInterfaceOrientation)
I've never heard about such sweet opportunity. So I think you should manually reload images.

UIImagePickerController differences from iPhone and iPad woes

I have an app designed for iPhone that makes use of the UIImagePickerController.
The app run in landscape only up until the image picker comes into view in its default portrait.
Problem 1:
On the iPhone when I rotate the device to portrait to view the image library the image picker view seems to do a flip as if rotating from landscape to portrait?
Problem 2:
I get the dreaded warning Using two-stage rotation animation. To use the smoother single-stage animation, this application must remove two-stage method implementations.
Problem 3.
on the iPad when selecting an image the image picker is dismissed and the view is back in landscape but ...... if I then do anything that requires alert view or the keyboard they appear as if the device is in portrait? This does not happen on the iPhone???
I know problem 2 is a long running issue but please can anyone help with problems 1 & 3?
Thanks
number 3 resolved :-)
"The keyboard will show up in the orientation of the statusbar, which doesn't always change with rotations for some reason. So if you want to display that view only in landscape set [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft; or whichever orientation you want to prefer in your viewWillAppear method."