首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在swift VC中以编程方式在代码中指定约束时,布局属性的配对无效

在swift VC中以编程方式在代码中指定约束时,布局属性的配对无效
EN

Stack Overflow用户
提问于 2016-04-26 12:59:19
回答 1查看 1.8K关注 0票数 0

我遵循示例中使用的模式:

https://github.com/lucasderraugh/AppleProg-Cocoa-Tutorials/blob/master/Lesson%2066/Lesson%2066/AppDelegate.swift

我的VC (NSViewController的子类)如下所示:

代码语言:javascript
复制
import Cocoa

class SecondViewController: NSViewController {

    var leftView = ColorView()
    var rightView = ColorView()

    @IBOutlet weak var nameTextField: NSTextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.


    }

    override func viewDidAppear() {

        leftView.translatesAutoresizingMaskIntoConstraints = false
        rightView.translatesAutoresizingMaskIntoConstraints = false



        let leftConstraints:[NSLayoutConstraint] = [

            leftView.widthAnchor.constraintEqualToAnchor(self.view.topAnchor),
            leftView.leftAnchor.constraintEqualToAnchor(self.view.leftAnchor),
            leftView.widthAnchor.constraintEqualToConstant(100),
            leftView.heightAnchor.constraintEqualToConstant(100)

        ]

        self.view.addSubview(leftView)

        NSLayoutConstraint.activateConstraints(leftConstraints)

    }

    override func prepareForSegue(segue: NSStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "SecondToFourthSegue" {
            (segue.destinationController as! NSViewController).representedObject = nameTextField.stringValue
        }
    }


}

但我在运行时得到以下错误:

布局属性的配对无效

完整堆栈跟踪:

代码语言:javascript
复制
2016-04-26 00:54:06.402 Hyperterm[19046:1591173] An uncaught exception was raised
2016-04-26 00:54:06.403 Hyperterm[19046:1591173] *** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Invalid pairing of layout attributes
2016-04-26 00:54:06.403 Hyperterm[19046:1591173] (
    0   CoreFoundation                      0x00007fff89cf94f2 __exceptionPreprocess + 178
    1   libobjc.A.dylib                     0x00007fff8b89b73c objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff89d604bd +[NSException raise:format:] + 205
    3   Foundation                          0x00007fff8d717f92 VerifyConstraintArguments + 356
    4   Foundation                          0x00007fff8d717c16 +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:] + 295
    5   Foundation                          0x00007fff8d71d410 -[NSLayoutAnchor constraintEqualToAnchor:multiplier:constant:] + 149
    6   Foundation                          0x00007fff8d71e87f -[NSLayoutDimension constraintEqualToAnchor:multiplier:constant:] + 39
    7   Hyperterm                           0x000000010001bee1 _TFC9Hyperterm20SecondViewController13viewDidAppearfT_T_ + 385
    8   Hyperterm                           0x000000010001c4b2 _TToFC9Hyperterm20SecondViewController13viewDidAppearfT_T_ + 34
    9   libdispatch.dylib                   0x0000000100607070 _dispatch_call_block_and_release + 12
    10  libdispatch.dylib                   0x00000001005f9cc5 _dispatch_client_callout + 8
    11  libdispatch.dylib                   0x00000001006116ae _dispatch_main_queue_callback_4CF + 2845
    12  CoreFoundation                      0x00007fff89cae9e9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    13  CoreFoundation                      0x00007fff89c6d8dd __CFRunLoopRun + 1949
    14  CoreFoundation                      0x00007fff89c6ced8 CFRunLoopRunSpecific + 296
    15  HIToolbox                           0x00007fff949a7935 RunCurrentEventLoopInMode + 235
    16  HIToolbox                           0x00007fff949a776f ReceiveNextEventCommon + 432
    17  HIToolbox                           0x00007fff949a75af _BlockUntilNextEventMatchingListInModeWithFilter + 71
    18  AppKit                              0x00007fff878aeefa _DPSNextEvent + 1067
    19  AppKit                              0x00007fff878ae32a -[NSApplication _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 454
    20  AppKit                              0x00007fff878a2e84 -[NSApplication run] + 682
    21  AppKit                              0x00007fff8786c46c NSApplicationMain + 1176
    22  Hyperterm                           0x000000010001b724 main + 84
    23  libdyld.dylib                       0x00007fff8fc285ad start + 1
    24  ???                                 0x0000000000000003 0x0 + 3
)
2016-04-26 00:54:06.405 Hyperterm[19046:1591173] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Invalid pairing of layout attributes'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff89cf94f2 __exceptionPreprocess + 178
    1   libobjc.A.dylib                     0x00007fff8b89b73c objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff89d604bd +[NSException raise:format:] + 205
    3   Foundation                          0x00007fff8d717f92 VerifyConstraintArguments + 356
    4   Foundation                          0x00007fff8d717c16 +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:] + 295
    5   Foundation                          0x00007fff8d71d410 -[NSLayoutAnchor constraintEqualToAnchor:multiplier:constant:] + 149
    6   Foundation                          0x00007fff8d71e87f -[NSLayoutDimension constraintEqualToAnchor:multiplier:constant:] + 39
    7   Hyperterm                           0x000000010001bee1 _TFC9Hyperterm20SecondViewController13viewDidAppearfT_T_ + 385
    8   Hyperterm                           0x000000010001c4b2 _TToFC9Hyperterm20SecondViewController13viewDidAppearfT_T_ + 34
    9   libdispatch.dylib                   0x0000000100607070 _dispatch_call_block_and_release + 12
    10  libdispatch.dylib                   0x00000001005f9cc5 _dispatch_client_callout + 8
    11  libdispatch.dylib                   0x00000001006116ae _dispatch_main_queue_callback_4CF + 2845
    12  CoreFoundation                      0x00007fff89cae9e9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    13  CoreFoundation                      0x00007fff89c6d8dd __CFRunLoopRun + 1949
    14  CoreFoundation                      0x00007fff89c6ced8 CFRunLoopRunSpecific + 296
    15  HIToolbox                           0x00007fff949a7935 RunCurrentEventLoopInMode + 235
    16  HIToolbox                           0x00007fff949a776f ReceiveNextEventCommon + 432
    17  HIToolbox                           0x00007fff949a75af _BlockUntilNextEventMatchingListInModeWithFilter + 71
    18  AppKit                              0x00007fff878aeefa _DPSNextEvent + 1067
    19  AppKit                              0x00007fff878ae32a -[NSApplication _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 454
    20  AppKit                              0x00007fff878a2e84 -[NSApplication run] + 682
    21  AppKit                              0x00007fff8786c46c NSApplicationMain + 1176
    22  Hyperterm                           0x000000010001b724 main + 84
    23  libdyld.dylib                       0x00007fff8fc285ad start + 1
    24  ???                                 0x0000000000000003 0x0 + 3
)
libc++abi.dylib: terminating with uncaught exception of type NSException
EN

回答 1

Stack Overflow用户

发布于 2016-04-26 13:52:11

leftView.widthAnchor不应等于self.view.topAnchor,您可能不会注意到它

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36855584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档