Bagusflyer
Bagusflyer

Reputation: 12915

Different layout for iPhone and iPad in the same Storyboard

Xcode 6 supports the "universal" storyboard. This is very convenient. That means we only need one storyboard for iPhone and iPad if the layout of iPhone and iPad are the same.But here comes a question:

What if most of the layouts of iPhone and iPad are the same, only a few differences in only one or two UIViewControllers? How can we add different UIViewControllers in the same storyboard for iPhone and iPad?

Upvotes: 2

Views: 1448

Answers (1)

Bagusflyer
Bagusflyer

Reputation: 12915

Here is what I did:

  1. Add a constraint for height of a container view (yellow part) in storyboard

enter image description here

  1. My code: (Add to navigation bar if in iPad. And add to the container view and also set the constants of the constraint to 0)

    class ViewController: UIViewController {
    
    var segmentControl : UISegmentedControl!
    @IBOutlet weak var segContainer: UIView!
    @IBOutlet weak var heightConstrains: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.segmentControl = UISegmentedControl(items: ["Personnal", "Department", "Company"]) as UISegmentedControl
    
        if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
            self.navigationItem.titleView = self.segmentControl
            self.heightConstrains.constant = 0
        } else {
            self.segContainer.addSubview(self.segmentControl)
        }
    }
    

My case is quite simple. So it's not difficult to implement in code. But what about the very complicated case?

Upvotes: 1

Related Questions