Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 自动布局编程调整问题:无法调整Superview的大小_Cocoa_Swift_Interface Builder_Autolayout - Fatal编程技术网

Cocoa 自动布局编程调整问题:无法调整Superview的大小

Cocoa 自动布局编程调整问题:无法调整Superview的大小,cocoa,swift,interface-builder,autolayout,Cocoa,Swift,Interface Builder,Autolayout,如果我在IB中定义约束,我仍然可以调整包含子视图的窗口的大小 所有其他约束都是相同的,但在左侧、右侧和底部。 当我尝试在代码中执行此操作时,我无法在运行时手动调整superview(NSWindow)的大小,也无法将其最大化: class ViewController: NSViewController { @IBOutlet var box : NSBox! func matchAttribute(attribute: NSLayoutAttribute, view

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


所有其他约束都是相同的,但在左侧、右侧和底部。
当我尝试在代码中执行此操作时,我无法在运行时手动调整superview(NSWindow)的大小,也无法将其最大化:

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中向框添加任何约束,在加载框时,它被认为具有不明确的布局。为了消除这种歧义,Cocoa会自动添加一些约束:

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)]>
}
override func viewDidLoad(){
super.viewDidLoad()
println(视图约束)
//-> 
//-> 
//-> 
//-> 
}
您将注意到,最后两个约束的组合效果是固定长方体的宽度和高度-这就是您无法调整窗口大小的原因,因为这样做会违反这些约束

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

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!)
}
override func viewDidLoad(){
super.viewDidLoad()
view.removeConstraints(view.constraints)
//禁止Cocoa自动将其自身的约束添加到此视图
view.translatesAutoresizingMaskIntoConstraints=false
//请注意,在您的实现中,您不需要输入
匹配属性(nslayouttribute.Top,视图:box,超级视图:box.superview!)
matchAttribute(NSLayoutAttribute.Trailing,视图:box,superview:box.superview!)
匹配属性(nslayouttribute.Leading,视图:box,超级视图:box.superview!)
匹配属性(nslayouttribute.Bottom,视图:box,超级视图:box.superview!)
}

刚刚尝试过这个:它工作得很好这在很多层面上都是一个惊人的答案。同样重要的是,它允许我在制图、VFL或其他方面进行约束。这让我完全不知所措。非常感谢您的帮助和完美的解释。