You can't achieve what you want to do in Interface Builder. But this is possible by creating constraints via code. I have created a sample project for you which you can download from Here
Below is the code of ViewController file to add constraints via code.
Note: I have added arbitrary constraints in Interface Builder since we need to get the main view height. Those are also important
//
// ViewController.swift
// Test
//
// Created by FR-MAC on 11/12/14.
// Copyright (c) 2014 ABC. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var mySubview: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.addContraintsForMoviePlayerView(self.mySubview, superView: self.view)
}
func addContraintsForMoviePlayerView(var view:UIView, superView:UIView) -> Void {
view.setTranslatesAutoresizingMaskIntoConstraints(false)
let leftConstraint = NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: superView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0)
let rightConstraint = NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: superView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: superView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)
var topValue:CGFloat = superView.frame.size.height
topValue = topValue/2
let topConstraint = NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view.superview?, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: topValue)
// Remove the constraints created in Interface Builder to avoid autolayout constraint issues. We just added those constraints to get the correct height of self.view.frame which we used to create above topConstaint
self.view.removeConstraints(self.view.constraints())
//Add the above created constraints
superView.addConstraints( [leftConstraint, rightConstraint, topConstraint, bottomConstraint] )
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Hope this helps you :)