子视图绑定到NSLayoutContraint中的父视图SWIFT

时间:2015-08-31 15:56:49

标签: ios xcode macos swift nslayoutconstraint

我对SWIFT和Mac开发非常陌生。现在我正在尝试使用NSLayoutContraint。

var boardView:NSView = NSView()
boardView.wantsLayer = true
boardView.layer!.backgroundColor = NSColor.redColor().CGColor

//boardView.setFrameSize(NSSize(width: 200, height: 200))
//boardView.setFrameOrigin(NSPoint(x: 50, y: 50))
self.view.addSubview(boardView)
self.view.addConstraint(NSLayoutConstraint(item: boardView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: .Width, multiplier: 0.8, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: boardView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: .Height, multiplier: 0.8, constant: 0))
println(self.view.frame)
println(boardView.frame)

这些代码保留在ViewController.viewDidLoad()中,但在运行代码后无法显示该窗口。控制台输出是:

(0.0, 0.0, 800.0, 600.0)
(0.0, 0.0, 0.0, 0.0)

然后我设置boardView的宽度和高度(取消注释第5,6行)。视图现在可以显示,但窗口大小是boardView的约束,而不是适合外窗的boardView。

现在我对此非常困惑。非常感谢您的帮助!

首次更新

我将println复制到viewDidLayout(),输出如下:

(0.0, 0.0, 800.0, 600.0)
(0.0, 0.0, 0.0, 0.0)
(0.0, 0.0, 0.0, 0.0)
(0.0, 0.0, 0.0, 0.0)

发生了一些错误/警告。

Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const: conn 0x4fc23 token 0x11ffffffffff2ad
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: Backtrace (at 790422):
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  0   CoreGraphics                        0x00007fff90873501 CGSBacktraceCreate + 59
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  1   CoreGraphics                        0x00007fff908c8244 _ZN16CGSUpdateManager14disable_updateEv + 84
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  2   AppKit                              0x00007fff97dcdd15 -[NSWindow disableScreenUpdatesUntilFlush] + 127
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  3   AppKit                              0x00007fff97dcc95a -[NSView(NSInternal) geometryInWindowDidChange] + 185
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  4   AppKit                              0x00007fff97dcc581 -[NSView _invalidateFocus] + 437
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  5   AppKit                              0x00007fff97de9e67 -[NSView setFrameSize:] + 1735
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  6   AppKit                              0x00007fff97e5f87e -[NSThemeFrame setFrameSize:] + 473
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  7   AppKit                              0x00007fff97de8f83 -[NSView setFrame:] + 464
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  8   AppKit                              0x00007fff97e5537f -[NSView resizeWithOldSuperviewSize:] + 1206
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  9   AppKit                              0x00007fff97f5bd15 -[NSView layoutSubtreeIfNeeded] + 810
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  10  AppKit                              0x00007fff97f5b8ba _NSViewLayoutSubtreesThatUseAutolayoutIfNeeded + 71
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  11  AppKit                              0x00007fff97f5b841 -[NSWindow(NSConstraintBasedLayout) _layoutViewTree] + 84
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  12  AppKit                              0x00007fff97f7ad3d -[NSWindow(NSConstraintBasedLayout) layoutIfNeeded] + 246
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  13  AppKit                              0x00007fff97e1f52e _handleWindowNeedsDisplayOrLayoutOrUpdateConstraints + 612
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  14  AppKit                              0x00007fff98555171 __83-[NSWindow _postWindowNeedsDisplayOrLayoutOrUpdateConstraintsUnlessPostingDisabled]_block_invoke1540 + 46
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  15  CoreFoundation                      0x00007fff9295d127 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  16  CoreFoundation                      0x00007fff9295d080 __CFRunLoopDoObservers + 368
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  17  CoreFoundation                      0x00007fff9294f188 __CFRunLoopRun + 872
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  18  CoreFoundation                      0x00007fff9294ebd8 CFRunLoopRunSpecific + 296
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  19  HIToolbox                           0x00007fff9900456f RunCurrentEventLoopInMode + 235
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  20  HIToolbox                           0x00007fff990041ee ReceiveNextEventCommon + 179
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  21  HIToolbox                           0x00007fff9900412b _BlockUntilNextEventMatchingListInModeWithFilter + 71
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  22  AppKit                              0x00007fff97e1c8ab _DPSNextEvent + 978
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  23  AppKit                              0x00007fff97e1be58 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 346
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  24  AppKit                              0x00007fff97e11af3 -[NSApplication run] + 594
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  25  AppKit                              0x00007fff97d8e244 NSApplicationMain + 1832
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  26  GameBegin_Mac                       0x000000010000df0d main + 109
Aug 31 19:14:33 Hans-Bogenmacher.local GameBegin_Mac[28780] <Warning>: void CGSUpdateManager::log() const:  27  libdyld.dylib                       0x00007fff967715c9 start + 1

第二次更新

我将约束更改为:

self.view.addConstraint(NSLayoutConstraint(item: boardView!, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: .CenterX, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: boardView!, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: .CenterY, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: boardView!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: .Width, multiplier: 0.8, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: boardView!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: .Height, multiplier: 0.8, constant: 0))

但它没用,控制台输出是:

(0.0, 0.0, 800.0, 480.0)
(0.0, 0.0, 0.0, 0.0)
(0.0, 0.0, 0.0, 0.0)
(0.0, 0.0, 0.0, 0.0)

第三次更新

只需调用boardView.translatesAutoresizingMaskIntoConstraints = false就行了!谢谢!

但现在我无法完成要求:

boardView.frame.width = self.view.frame.width * 0.8
boardView.frame.centerX = self.view.frame.centerX
boardView.frame.height = self.view.frame.height * 0.1
boardView.frame.top = self.view.frame.height * 0.2

现在我知道如何做第1,2,3行,但是我不能将Top到高度绑定,如何进行第4行?谢谢!

0 个答案:

没有答案