Autolayout:设置约束后无法调整窗口大小

时间:2015-06-04 00:13:46

标签: macos cocoa autolayout

我需要一些以编程方式设置自动布局的帮助。我有一个观点,我设置了另一个由故事板管理的视图。

var content = NSView(frame: NSRect(x: 0, y: 0, width: 3000, height: self.view.frame.height))
[...]
scrollingView.documentView = content

比我添加视图永远是从上到下的引脚约束:

    let topPinContraints = NSLayoutConstraint(
        item: contentView
        , attribute: NSLayoutAttribute.Top
        , relatedBy: .Equal
        , toItem: contentView.superview
        , attribute: NSLayoutAttribute.Top
        , multiplier: 1.0
        , constant: 0
    )
    let bottomPinContraints = NSLayoutConstraint(
          item: contentView
        , attribute: NSLayoutAttribute.Bottom
        , relatedBy: .Equal
        , toItem: contentView.superview
        , attribute: NSLayoutAttribute.Bottom
        , multiplier: 1.0
        , constant: 0
    )
    let heightContraints = NSLayoutConstraint(
        item: contentView
        , attribute: NSLayoutAttribute.Height
        , relatedBy: .Equal
        , toItem: contentView.superview
        , attribute: NSLayoutAttribute.Height
        , multiplier: 1.0
        , constant: 0
    )

    NSLayoutConstraint.activateConstraints([
          topPinContraints
        , bottomPinContraints
        , heightContraints
    ])

    contentView.needsLayout = true

在NSLayoutConstraint.activateConstraints之后,视图在superview中从上到下填充,窗口的高度不再可调整。 我可以在宽度上调整它的大小,但现在高度已经固定。

如何在不失去调整大小的情况下添加顶部和底部引脚?

非常感谢,

PS。

1 个答案:

答案 0 :(得分:1)

最后我有一些适合我的代码。但这是正确的解决方案吗?让我发布我的整个ViewController来向您展示我的经验。也许你可以发表你的意见,我可以做得更好。

import Foundation
import Cocoa

class ColoumnMasterViewController: NSViewController {

@IBOutlet weak var scrollingView: NSScrollView!

var position = 0
var contentView: NSView?
var widthtContraints: NSLayoutConstraint?

override func viewDidLoad() {
    contentView = NSView(frame: NSRect(x: 0, y: 0, width: 3000, height: self.view.frame.height))

    contentView!.wantsLayer = true
    contentView!.layer?.backgroundColor =  NSColor.redColor().CGColor

    add()
    var timer = NSTimer.scheduledTimerWithTimeInterval(1.6, target: self, selector: Selector("add"), userInfo: nil, repeats: true)

    scrollingView.documentView = contentView

    autoLayout(contentView!)
}

func add(){
    println("add view on position \(position)")
    let vc: NSViewController = self.storyboard?.instantiateControllerWithIdentifier("coloumn_view_controller") as! NSViewController
    vc.view.setFrameOrigin(NSPoint(x: (position++ * 130), y: 0))
    vc.view.setFrameSize(NSSize(width: 130, height: self.view.frame.height))
    contentView!.addSubview(vc.view)

    updateWidth()
}

func autoLayout(contentView: NSView){
    let topPinContraints = NSLayoutConstraint(
        item: contentView
        , attribute: NSLayoutAttribute.Top
        , relatedBy: .Equal
        , toItem: contentView.superview
        , attribute: NSLayoutAttribute.Top
        , multiplier: 1.0
        , constant: 0
    )
    let bottomPinContraints = NSLayoutConstraint(
          item: contentView
        , attribute: NSLayoutAttribute.Bottom
        , relatedBy: .Equal
        , toItem: contentView.superview
        , attribute: NSLayoutAttribute.Bottom
        , multiplier: 1.0
        , constant: 0
    )

    println("heightContraints \(contentView.superview!.frame.height)")
    let heightContraints = NSLayoutConstraint(
        item: contentView
        , attribute: NSLayoutAttribute.Height
        , relatedBy: .Equal
        , toItem: contentView.superview!
        , attribute: NSLayoutAttribute.Height
        , multiplier: 1.0
        , constant: 0
    )
    println("widthtContraints \(contentView.superview!.frame.width)")
    let calculatedWith: CGFloat = (CGFloat) (contentView.subviews.count * 130)
    widthtContraints = NSLayoutConstraint(
        item: contentView
        , attribute: NSLayoutAttribute.Width
        , relatedBy: .Equal
        , toItem: nil
        , attribute: NSLayoutAttribute.Width
        , multiplier: 1.0
        , constant: 0
    )
    widthtContraints!.constant = calculatedWith

    NSLayoutConstraint.activateConstraints([
          topPinContraints
         , bottomPinContraints
         , heightContraints
         , widthtContraints!
    ])

    contentView.translatesAutoresizingMaskIntoConstraints = false
    contentView.needsLayout = true

}

func updateWidth(){
    let calculatedWith: CGFloat = (CGFloat) (contentView!.subviews.count * 130)
    if (widthtContraints != nil) {
        widthtContraints!.constant = calculatedWith
    }
}

}

非常感谢,

PS