我需要一些帮助与自动布局设置编程。我将一个视图设置为另一个由storyboard管理的视图。
var content = NSView(frame: NSRect(x: 0, y: 0, width: 3000, height: self.view.frame.height))
[...]
scrollingView.documentView = content
然后我添加pin约束,视图将始终从上到下:
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。最后我有一些为我工作的代码。但这是正确的解决方案吗?我把整个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
}
}
}
万分感谢,
p