Limit to portrait or landscape depending on device? Possible?

I have a timer app that really only fits on a phone in landscape given that it shows a sports playing field (think rectangle).

On an iPad it would work easily–even in split screen if the person wanted to run iTunes or other apps at the same time as we often do when working the booth (we’re talking hockey).

Can I prohibit portrait on a phone but allow any layout on an iPad? the IB doesn’t show it, but I suspect it’s possible programmatically.

Hi you can check on the user device (see below) and set constraints programatically.

Blessings,
—Mark

if UIDevice.current.userInterfaceIdiom == .pad {
            
        } else if UIDevice.current.userInterfaceIdiom == .phone {
            
        }

Hi and thanks,

Do you mean you can set constraints in terms of portrait / landscape or just constraints (positionally)?

Can you give one example?

Hi,

Yes, you will want to set constraints depending on the orientation. You can check the device orientation with the following:

if UIApplication.shared.statusBarOrientation.isLandscape {
            // activate landscape changes
        } else {
            // activate portrait changes
        }

You can then set the constraints programmatically depending on what you want the user interface to look like.

Here is an example of doing this with an imageView:

view.addSubview(cardImageView) //don't forget to add the object to the view. I forget this all the time!
        cardImageView.translatesAutoresizingMaskIntoConstraints = false
        cardImageView.image = UIImage(named: "TestPict")
        
        NSLayoutConstraint.activate([
            cardImageView.widthAnchor.constraint(equalToConstant: 250),
            cardImageView.heightAnchor.constraint(equalToConstant: 350),
            cardImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            cardImageView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -75),
        ])

Blessings,
—Mark

Thanks,

the addsubview is only when doing this programmatically correct?

Yes
Programmatically adding a something to a view is used with addSubView, and view.addSubView() the view is referring to adding a new that ViewController

This is all part of the view hierarchy