Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView does not have a member named 'setTranslatesAutoresizingMaskIntoConstraints'

I am Using Xcode 7 beta & Swift 2

I am trying to add a ViewController (childVC) to a container through an addController action. I want to set auto layout for ViewController with respect to the container. In the below code it gives the following error

UIView does not have a member named 'setTranslatesAutoresizingMaskIntoConstraints'. I have also tried putting 'false' in bracket (see the commented line below) -- but even that does not work

I basically want the childVC to occupy the entire container. The childVC has a tableview which should resize depending on the container size.

func addController(controller: UIViewController)
{
    addChildViewController(controller)
    containerView.addSubview(controller.view)


   controller.view.setTranslatesAutoresizingMaskIntoConstraints = false

   // controller.view.setTranslatesAutoresizingMaskIntoConstraints(false)
    var constraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|[view]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view" : controller.view])
    constraints += NSLayoutConstraint.constraintsWithVisualFormat("V:|[view]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view" : controller.view])
    NSLayoutConstraint.activateConstraints(constraints)
    didMoveToParentViewController(controller)
    currentController = controller

}
like image 361
Anuj Arora Avatar asked Aug 26 '15 15:08

Anuj Arora


3 Answers

Prior to iOS 9, setTranslatesAutoresizingMaskIntoConstraints was a function:

func setTranslatesAutoresizingMaskIntoConstraints(_ flag: Bool)

In iOS 9 it became a property:

 var translatesAutoresizingMaskIntoConstraints: Bool 

You will have to decide if you are only targeting iOS 9, set the deployment target accordingly, and use the property. If you are supporting older versions of iOS you can use the new Availability feature of Swift 2.

if #available(iOS 9, *) {
    controller.view.translatesAutoresizingMaskIntoConstraints = false
} else {
    controller.view.setTranslatesAutoresizingMaskIntoConstraints(false)
}
like image 172
Mr Beardsley Avatar answered Nov 17 '22 05:11

Mr Beardsley


In Swift 2, the setTranslatesAutoresizingMaskIntoConstraints method became a property - translatesAutoresizingMaskIntoConstraints.

This is nothing to do with API versions, and availability checks (which are also new in Swift 2) are completely unnecessary.

If utilising Swift 2 simply set the property value within your code base. If upgrading from Swift 1, you might see errors where the previous function call needs to be updated.

Simple.

like image 28
Thomas Clowes Avatar answered Nov 17 '22 03:11

Thomas Clowes


I think that the accepted answer is not correct. At least it does not compile on Xcode 7. As mentioned in one of the comments below the answer.

It is just enough to use property syntax, works both on iOS 8 and iOS 9:

controller.view.translatesAutoresizingMaskIntoConstraints = false

I think that this is because UIView is still Objective-C class and setting property on such a code will anyway invoke setTranslatesAutoresizingMaskIntoConstraints on older versions of iOS.

Moreover, the Apple documentation defines this as a property:

var translatesAutoresizingMaskIntoConstraints: Bool

This syntax is available since iOS 6, so I think that using #available is even not recommended here.

For reference please see: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instp/UIView/translatesAutoresizingMaskIntoConstraints

like image 33
tgebarowski Avatar answered Nov 17 '22 05:11

tgebarowski