Cannot convert value of type UILayoutPriority

Viewed 3510

I'm trying to change the priority of a constraint to be higher than that of another constraint. In Swift 4 / iOS 11, this code no longer compiles:

let p = self.lab2.contentCompressionResistancePriority(for:.horizontal)
self.lab1.setContentCompressionResistancePriority(p+1, for: .horizontal)

How am I supposed to do this?

2 Answers

You can do the following to continue using them as before:

import UIKit

// MARK: - Allow to pass a float value to the functions depending on UILayoutPriority
extension UILayoutPriority: ExpressibleByFloatLiteral {
    public typealias FloatLiteralType = Float

    public init(floatLiteral value: Float) {
        self.init(value)
    }
}

// MARK: - Allow to pass an integer value to the functions depending on UILayoutPriority
extension UILayoutPriority: ExpressibleByIntegerLiteral {
    public typealias IntegerLiteralType = Int

    public init(integerLiteral value: Int) {
        self.init(Float(value))
    }
}

Refer to https://gist.github.com/jeremiegirault/7f73692a162b6ecf8ef60c7809e8679e for a full implementation

Related