不能以编程方式设置NSLayoutConstraint乘数迅速..."不能分配给该前pression的结果 [英] Can't programmatically set NSLayoutConstraint multiplier in swift... "Cannot assign to the result of this expression

查看:508
本文介绍了不能以编程方式设置NSLayoutConstraint乘数迅速..."不能分配给该前pression的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式设置为迅速事半功倍的约束,当我设置的值,它只是给我的错误,不能分配给该前pression的结果...

I am trying to programmatically set the constraint for a multiplier in swift, and when I set the value, it just gives me the error, "Cannot assign to the result of this expression"...

我声明了一个IBOutlet的NSLayoutConstraint,然后设置倍频,同我不断的另一个原因是,它工作得很好做,但是这一次也不会接受......

I declared the NSLayoutConstraint with an IBOutlet, and then set the multiplier, same as I did with the constant for another, which works fine, but this one won't accept it...

@IBOutlet weak var clockWidthConstraint: NSLayoutConstraint!

override func updateViewConstraints() {
    super.updateViewConstraints()

    confirmTopConstraint.constant = 40.0
    clockWidthConstraint.multiplier = 0.1  // Gives me the error!

    }

任何想法?

推荐答案

是啊,这是<一个href=\"https://developer.apple.com/library/mac/documentation/AppKit/Reference/NSLayoutConstraint_Class/index.html#//apple_ref/occ/instp/NSLayoutConstraint/multiplier\"相对=nofollow>只读:

var multiplier: CGFloat { get } 

您只能指定在创建时的乘数。相反,你可以在运行时更改非恒定属性:

You can only specify the multiplier at creation time. In contrast, you can change the non-constant constant property at run-time:

var constant: CGFloat

编辑:

这需要一点点努力,但要改变一成不变的属性,您必须创建一个新的约束,复制所有,但你要更改的属性。我一直在玩弄这种不同的技术,但我目前正在探索这种形式:

It takes a little effort, but to change immutable properties, you have to create a new constraint and copy all but the property that you want to change. I keep playing around with different techniques for this, but am currently exploring this form:

let constraint = self.aspectRatioConstraint.with() {
    (inout c:NSLayoutConstraint.Model) in
    c.multiplier = self.aspectRatio }

或以更加逼真的上下文中使用的

or used in a more realistic context:

var aspectRatio:CGFloat = 1.0 { didSet {

    // remove, update, and add constraint
    self.removeConstraint(self.aspectRatioConstraint)
    self.aspectRatioConstraint = self.aspectRatioConstraint.with() {
        (inout c:NSLayoutConstraint.Model) in
        c.multiplier = self.aspectRatio }
    self.addConstraint(self.aspectRatioConstraint)
    self.setNeedsLayout()
}}

,其中后备code是:

Where the backing code is:

extension NSLayoutConstraint {

    class Model {

        init(item view1: UIView, attribute attr1: NSLayoutAttribute,
            relatedBy relation: NSLayoutRelation,
            toItem view2: UIView?, attribute attr2: NSLayoutAttribute,
            multiplier: CGFloat, constant c: CGFloat,
            priority:UILayoutPriority = 1000) {
                self.firstItem = view1
                self.firstAttribute = attr1
                self.relation = relation
                self.secondItem = view2
                self.secondAttribute = attr2
                self.multiplier = multiplier
                self.constant = c
                self.priority = priority
        }

        var firstItem:UIView
        var firstAttribute:NSLayoutAttribute
        var relation:NSLayoutRelation
        var secondItem:UIView?
        var secondAttribute:NSLayoutAttribute
        var multiplier:CGFloat = 1.0
        var constant:CGFloat = 0
        var priority:UILayoutPriority = 1000
    }


    func priority(priority:UILayoutPriority) -> NSLayoutConstraint {
        self.priority = priority;
        return self
    }

    func with(configure:(inout Model)->()) -> NSLayoutConstraint {

        // build and configure model
        var m = Model(
            item: self.firstItem as! UIView, attribute: self.firstAttribute,
            relatedBy: self.relation,
            toItem: self.secondItem as? UIView, attribute: self.secondAttribute,
            multiplier: self.multiplier, constant: self.constant)
        configure(&m)

        // build and return contraint from model
        var constraint = NSLayoutConstraint(
            item: m.firstItem, attribute: m.firstAttribute,
            relatedBy: m.relation,
            toItem: m.secondItem, attribute: m.secondAttribute,
            multiplier: m.multiplier, constant: m.constant)
        return constraint
    }
}

这篇关于不能以编程方式设置NSLayoutConstraint乘数迅速...&QUOT;不能分配给该前pression的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆