ipad constraint and orientations

I have an App for Iphone and Ipad.

in my UIViewController, the subviews installed depend of the different classes (CR, RC, CC, RR) because possible widths of the view are not the same.

This work very well on Iphone, because the width of the view is different, depending on the class (CR, RC, CC).

But for Ipad, the class is RR, no matter the orientation. Or in portrait and landscape, of course, width is not the same. In Interface Builder, I don't find a way to have subviews installed for Ipad in landscape and not in Portrait.

Is there a way to handle this, or do I have to handle this programatically?

Accepted Reply

Your ,last comment gave me an idea Claude. if I have 2 differnt storyboard for my UITableViewCell, I can write this in the

tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

of the uiViewController:

let cell : UITableViewCell

if view.traitCollection.className == "RR" {
          if UIScreen.main.bounds.size.width < UIScreen.main.bounds.size.height{
                    cell = table.getCell(indexPath) as! class1
          } else {
                     cell = table.getCell(indexPath) as! class2
          }
}

where class2 is a descendant of class1

Class2 if a descendant of class1, and can have other UIView installed and as class1 and class2 don't have the same storyboard, the constraints wh-ill not be the same. I have to precise that I wrote the following extension;

extension UITraitCollection { public var className: String { get { return (horizontalSizeClass == .compact ? "C" : "R") + (verticalSizeClass == .compact ? "C" : "R") } } }

  • Thanks for feedback, that's another option. Does it work well ? If so, don't forget to close the thread.

Add a Comment

Replies

I think you have to do it programmatically (at least, I had to).

I did it this way (after some try and error):

// 1. Define an enum Credit https://stackoverflow.com/questions/29528661/ ios-detect-current-size-classes-on-viewdidload
enum DeviceTraitStatus {
    // IPAD and others: Width: Regular, Height: Regular
    case wR
    case wC
    case hR
    case hC
    case wRhR
    // Any IPHONE Portrait Width: Compact, Height: Regular
    case wChR
    // IPHONE Plus/Max Landscape Width: Regular, Height: Compact
    case wRhC
    // IPHONE landscape Width: Compact, Height: Compact
    case wChC

    static var current:DeviceTraitStatus {

        switch (UIScreen.main.traitCollection.horizontalSizeClass, UIScreen.main.traitCollection.verticalSizeClass){

        case (UIUserInterfaceSizeClass.regular, UIUserInterfaceSizeClass.unspecified):
            return .wR
        case (UIUserInterfaceSizeClass.compact, UIUserInterfaceSizeClass.unspecified):
            return .wC
        case (UIUserInterfaceSizeClass.unspecified, UIUserInterfaceSizeClass.regular):
            return .hR
        case (UIUserInterfaceSizeClass.unspecified, UIUserInterfaceSizeClass.compact):
            return .hC

        case (UIUserInterfaceSizeClass.regular, UIUserInterfaceSizeClass.regular):
            return .wRhR
        case (UIUserInterfaceSizeClass.compact, UIUserInterfaceSizeClass.regular):
            return .wChR
        case (UIUserInterfaceSizeClass.regular, UIUserInterfaceSizeClass.compact):
            return .wRhC
        case (UIUserInterfaceSizeClass.compact, UIUserInterfaceSizeClass.compact):
            return .wChC
        default:
            return .wChR
        }
    }
}

// 2. Get device status
        let dts = DeviceTraitStatus.current

// 3. Get screenSize
    let wScreen = UIScreen.main.bounds.size.width
    var hScreen = UIScreen.main.bounds.size.height

// 4. Then test for all possible height regular, and find if iPad :
        if dts == .wRhR || dts == .wChR || dts == .hR {
            if wScreen >= 400 && hScreen >= 768 { // Do what you need to adjust constraints IBOutlets
            }
        }

// 5. Then test for all possible height compact, and find if iPad :
        if dts == .wRhC || dts == .wChC || dts == .hC {
            if hScreen >= 540 && wScreen >= 1000 { { // Do what you need to adjust constraints IBOutlets
            }
        }

You will have to check precisely the sizes (see here: https://www.ios-resolution.com/), for instance:

    //      12ProMax
    //          Portrait wChR  h 879.0 w 428.0
    //          Landscape wRhC   h 428.0 w 926.0
    //      12Pro
    //          Portrait wChR  h 797.0 w 390.0
    //          Landscape wChC   h 390.0 w 844.0

Yes, I thought of something like this But it means that I have to make constraints programatically for class RR, without capable of using functionallities of IB Thank's Claude

  • Yes, I faced a similar problem and could not find a better solution. Except creating 2 independent storyboards, but that causes a lot of complication.

Add a Comment

Your ,last comment gave me an idea Claude. if I have 2 differnt storyboard for my UITableViewCell, I can write this in the

tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

of the uiViewController:

let cell : UITableViewCell

if view.traitCollection.className == "RR" {
          if UIScreen.main.bounds.size.width < UIScreen.main.bounds.size.height{
                    cell = table.getCell(indexPath) as! class1
          } else {
                     cell = table.getCell(indexPath) as! class2
          }
}

where class2 is a descendant of class1

Class2 if a descendant of class1, and can have other UIView installed and as class1 and class2 don't have the same storyboard, the constraints wh-ill not be the same. I have to precise that I wrote the following extension;

extension UITraitCollection { public var className: String { get { return (horizontalSizeClass == .compact ? "C" : "R") + (verticalSizeClass == .compact ? "C" : "R") } } }

  • Thanks for feedback, that's another option. Does it work well ? If so, don't forget to close the thread.

Add a Comment