自动布局的程序调整大小问题:无法调整Superview的大小 [英] Autolayout Programatic Sizing Woes: Cannot Resize Superview

查看:96
本文介绍了自动布局的程序调整大小问题:无法调整Superview的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在IB中定义约束,我仍然可以调整容纳子视图的窗口的大小。

If I define my constraints in IB, I can still resize the window that houses the subview.





所有其他约束都相同,但分别在左,右和下。



当我尝试在代码中执行此操作时,无法再在运行时手动调整超级视图(NSWindow)的大小,也无法使其最大化:

All the other constraints are the same, but to left, right, and bottom.


When I try to do this in code, I can no longer resize the superview (NSWindow) by hand at runtime, nor maximize it:

class ViewController: NSViewController {

    @IBOutlet var box : NSBox!

    func matchAttribute(attribute: NSLayoutAttribute, view: NSView, superview: NSView) {
        var constraint = NSLayoutConstraint(item: view, attribute: attribute, relatedBy: NSLayoutRelation.Equal, toItem: superview, attribute: attribute, multiplier: 1, constant: 0)
        constraint.priority = 1000
        superview.addConstraint(constraint)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.matchAttribute(NSLayoutAttribute.Top, view: self.box, superview: self.box.superview!)
        self.matchAttribute(NSLayoutAttribute.Trailing, view: self.box, superview: self.box.superview!)
        self.matchAttribute(NSLayoutAttribute.Leading, view: self.box, superview: self.box.superview!)
        self.matchAttribute(NSLayoutAttribute.Bottom, view: self.box, superview: self.box.superview!)
    }
}

我该怎么做才能获得相同的自动版式

推荐答案

因为此时您没有在Interface Builder中向框添加任何约束它加载时被认为具有不明确的布局。要消除这种歧义,可可会自动添加一些约束:

Because you aren't adding any constraints to your box in Interface Builder, at the point it loads it is considered to have an ambiguous layout. To remove this ambiguity, Cocoa automatically adds some constraints:

override func viewDidLoad() {
    super.viewDidLoad()

    println(view.constraints)
    //-> <NSIBPrototypingLayoutConstraint:0x600000082e40 'IB auto generated at build time for view with fixed frame' H:|-(0)-[NSBox:0x100716940'This Is A Box'](LTR)   (Names: '|':AutoStack.PPView:0x100516120 )>
    //-> <NSIBPrototypingLayoutConstraint:0x600000082ee0 'IB auto generated at build time for view with fixed frame' V:|-(0)-[NSBox:0x100716940'This Is A Box']   (Names: '|':AutoStack.PPView:0x100516120 )>
    //-> <NSIBPrototypingLayoutConstraint:0x600000082f80 'IB auto generated at build time for view with fixed frame' H:[NSBox:0x100716940'This Is A Box'(480)]>
    //-> <NSIBPrototypingLayoutConstraint:0x600000083020 'IB auto generated at build time for view with fixed frame' V:[NSBox:0x100716940'This Is A Box'(270)]>
}

您会注意到,最后两个约束的综合作用是框的宽度和高度-这就是为什么您不能调整窗口大小,因为这样做会违反这些限制。

You'll note that the combined effect of the last two constraints is to fix the width and height of the box - this is why you cannot resize the window, because doing so would violate these constraints.

要获得所需的效果,首先需要删除这些自动生成的约束:

To get the effect you want, you first need to remove these auto-generated constraints:

override func viewDidLoad() {
    super.viewDidLoad()

    view.removeConstraints(view.constraints)
    // Forbid Cocoa from automatically adding its own constraints to this view 
    view.translatesAutoresizingMaskIntoConstraints = false

    // Note that in your implementation you are needlessly typing <self>
    matchAttribute(NSLayoutAttribute.Top, view: box, superview: box.superview!)
    matchAttribute(NSLayoutAttribute.Trailing, view: box, superview: box.superview!)
    matchAttribute(NSLayoutAttribute.Leading, view: box, superview: box.superview!)
    matchAttribute(NSLayoutAttribute.Bottom, view: box, superview: box.superview!)
}

这篇关于自动布局的程序调整大小问题:无法调整Superview的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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