SwiftUI - Deal with regular/regular size classes on iPad - ipad

I need to make a different layout for iPad on landscape and portrait orientations, but size classes on iPad are always regular/regular. Recommended way to adapt interfaces by Apple is to use adaptive layout with size classes, but on iPad there is no difference between portrait and landscape mode.
Is there any way to present different layout on portrait and landscape orientation on iPad without detection device orientation?
As a example, on iPhone (compact/regular) will be shown as:
And on iPad, in landscape (regular/regular) will be shown as:
So, the goal is show the iPhone layout on iPad when is in portrait mode.
Thanks!

You can force apply horizontal size class depending on orientation. If I correctly understood your goal here is a demo of possible approach (tested with Xcode 12.1 / iOS 14.1):
struct DemoViewSizes: View {
#Environment(\.horizontalSizeClass) var horizontalSizeClass
#State private var orientation = UIDevice.current.orientation
var body: some View {
Text("root_view_here")
.onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
orientation = UIDevice.current.orientation
}
.environment(\.horizontalSizeClass, orientation == .portrait ? .compact : .regular)
}
}

Related

Change view based on device - SwiftUI

I have designed my app initially for the iPad and am now wanting to add functionality for an iPhone too. Is there a way to check what the device being used is, and then display a view accordingly?
Structured English:
IF current_device == iPhone THEN
DISPLAY iPhoneView
ELSE IF current_device == iPad THEN
DISPLAY iPadView
If possible I also want the iPad view to only be available horizontally and then the iPhone view to only be available vertically if possible.
What you are looking for are Size Classes.
To read current horizontal size class in SwiftUI view you can refer to the environment value of horizontalSizeClass
#Environment(\.horizontalSizeClass) var horizontalSizeClass
Then use it like this in your SwiftUI View:
var body: some View {
if horizontalSizeClass == .compact {
return CompactView() // view laid out for smaller screens, like an iPhone
} else {
return RegularView() // view laid out for wide screens, like an iPad
}
}
It is worth noting that not all iPhones are compact horizontally, and compact size class is present on iPad while in multitasking configuration. You will find all possible combinations here under the Device Size Classes and Multitasking Size Classes sections.
Some articles that may be helpful
How to create different layouts using size classes
Changing a view’s layout in response to size classes
Alternatively you could set an individual threshold based on the devices height (or width) using a variable like this:
#State var isLargeDevice: Bool = {
if UIScreen.main.bounds.height > 800 {
return true
} else {
return false
}
}()

How can I remove views using vary of traits?

I have a view controller with some nested views in portrait mode, but I need to know if its posible to generate a variation on landscape where I only have one image (deleting all my elements that I have in portrait view) or I need to create another view controller for this case.
you can change that in code using traitcollection
For your case
you can use below condition which presents landscape orientation
if traitCollection.verticalSizeClass == .compact {
labelName.isHidden = true // hide label
textfield.isHidden = true // hide text
imageName.isHidden = false // unhide image
}
Note: you have also traitcollection.horizontalSizeClass and it can be .compact or .regular according to which orientation of the device you want to edit and the type of device you are working on.
traitcollection options for different devices

iOS: Size classes and device orientation at launch

Here is my problem: rotate an iPhone 6 Plus or an iPad to e.g. LandscapeLeft.
Lets say your app has the following code (only for example)
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
// mine does use All, but with this case you'll understand my question
}
How do I detect if the device should use vertical or horizontal layout when
func willTransitionToTraitCollection(newCollection: UITraitCollection, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
is called?
print(UIScreen.mainScreen().bounds.width < UIScreen.mainScreen().bounds.height)
// will return false, because we started in landscape but the layout will be forced to be vertical here
print(UIApplication.sharedApplication().statusBarOrientation == .LandscapeLeft)
// will return true, because we started in landscape but the layout will be forced to be vertical here
The main problem appears on the iPad, because both size classes are regular.
I'm confused and have no idea how to solve this issue.
Don't think of your layouts as vertical and horizontal. Just let the app rotate, and respond to the size class or the size change.
On the iPad, try to avoid having two layouts. Just use autolayout to rejigger the proportions automatically. But if you must respond to rotation on the iPad, then respond to viewWillTransitionToSize (because the trait collection won't change).

Changing font size in a label for only iPhone 4s, is this possible?

I'm working in Portrait mode. I'm looking for a way to change the font size of text in a label so it only applies to the iPhone 4s. I understand that regular height/compact width applies to the iPhone 4s, 5, and 6. But with my app the way it is, I have to make my font size so small just to make it 4s compatible. On the 5 it looks alright, but it makes viewing it on the 6 very very ugly (being of how miniscule it looks). What would you guys do in this situation?
Is there any way to do this programmatically even? If this isn't possible, how could Apple miss this? Doesn't it make more sense to put the 4s in it's own subcategory with regards to it's height?
I am programming in Swift.
Detecting the device model:
if UIDevice.currentDevice().model == "iPhone4,1" {
// do whatever you want here for iPhone 4S
} else {
// other devices
}
nativeBounds
The bounding rectangle of the physical screen, measured in pixels.
(read-only)
Declaration SWIFT
var nativeBounds: CGRect { get }
This rectangle is based on the device in a portrait-up orientation. This
value does not change as the device rotates.
Import Statement
import UIKit
Detecting the device's height:
if UIScreen.mainScreen().nativeBounds.height == 960.0 {
}
Detecting the device's width:
if UIScreen.mainScreen().nativeBounds.width == 640.0 {
}

resize the image according to the portrait and landscape in jquerymobile?

I have to add image on the header and background . Is it possible to set the image automatically resizing according to the screen orientation (portrait and Landscape). I set the image in portrait resolution , But when i change the screen to Landscape the image not resized. Can anyone give me some guidelines?
Support for this is built into jQuery Mobile. There are two Orientation Classes which are present depending on the current screen orientation:
.portrait {
/* portrait orientation changes go here! */
}
.landscape {
/* landscape orientation changes go here! */
}
Or if you want a javascript event you can bind to orientationchange.
I don't think there are events that get triggered when the phone goes for landscape to portrait.You can however write a custom function to find out the current orientation. Write the following code on window.resize event.
$(window).resize( function(){
var height = $(window).height();
var width = $(window).width();
if(width>height) {
// Landscape
$("#img").attr("src","landscapeurl");
} else {
// Portrait
$("#img").attr("src","portraiturl");
}
});
code from Here
It's recommended to use CSS3 media queries as the jQuery mobile orientation classes are deprecated: http://jquerymobile.com/demos/1.0b2/docs/api/mediahelpers.html
For a demo of CSS3 media queries: http://www.webdesignerwall.com/demo/media-queries/
i use this css3 rule:
#media (orientation: landscape) {
#home-feature span {
display : inline-block;
}
}
more info: http://www.w3.org/TR/css3-mediaqueries/

Resources