2015-06-04 58 views
0

我需要一些編程方式的自動佈局設置幫助。我有一種觀點,我設置了另一個由故事板管理的視圖。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後視圖被從頂部填充底部在上海華,的窗戶的高度不再可調。 我可以調整它的寬度,但高度現在是固定的。

如何在不失去調整大小的情況下添加頂部和底部引腳?

非常感謝,

ps。

+0

是'content'(在你的第一個片段)和'contentView'(在你的第二個)是一回事嗎?你是否在視圖上將'translatesAutoresizingMaskIntoConstraints'設置爲false?另外,如果'documentView'被固定到其超視圖,即剪輯視圖,滾動視圖將在什麼情況下滾動?也就是說,文檔視圖何時與滾動視圖的內容大小不一樣大? –

+0

嗨肯,是的,和斯里。 contentView = view,因爲我將它命令爲func:func autoLayout(contentView:NSView){}。當我設置contentView.translatesAutoresizingMaskIntoConstraints = false時,視圖會完全消失。 –

+0

我可以設置heightContraints和widthtContraints沒有contentView.translatesAutoresizingMaskIntoConstraints,但比scrollview不工作了,因爲寬度比contentView.superview!.frame.with我想。 :-( –

回答

0

最後我有一些代碼適合我。但它是正確的解決方案嗎?讓我發佈我的整個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