自动布局约束在生产中引发异常

Posted

技术标签:

【中文标题】自动布局约束在生产中引发异常【英文标题】:Autolayout constraint throws exception in production 【发布时间】:2015-04-19 14:10:29 【问题描述】:

我遇到了一个非常奇怪的问题,我似乎无法弄清楚如何开始工作。当我在我的设备或模拟器上调试我的应用程序时,我没有收到任何错误。当我为临时部署构建它时,该应用程序会引发异常并失败。我在日志中遇到的错误是

[NSISEngine tryToAddConstraintWithMarker:expression:integralizationAdjustment:mutuallyExclusiveConstraints:]

对于我的所有视图,我的布局看起来都是这样的。

我的约束将视图扩展到屏幕的整个宽度,并且我在 BaseViewController 中的视图上创建了一个动态宽度约束,该约束被所有其他视图继承。我已经为我的每个子视图的 scrollView 和 contentView 创建了我的代表。

class BaseViewController: UIViewController, UITextFieldDelegate 
var activeField:UITextField?

@IBOutlet weak var scrollView: UIScrollView?
@IBOutlet weak var contentView: UIView?

override func viewDidLoad() 
    if(self.scrollView != nil)
    
        // Do any additional setup after loading the view.
        let leftConstraint:NSLayoutConstraint = NSLayoutConstraint(item: self.contentView!, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0)

        self.view.addConstraint(leftConstraint)

        let rightConstraint:NSLayoutConstraint = NSLayoutConstraint(item: self.contentView!, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0)

        self.view.addConstraint(rightConstraint)
    

    super.viewDidLoad()

我非常乐意提供任何其他信息,请尽管询问,我会尽我所能。

编辑

这只发生在未连接到调试器的存档和运行时。如果我调试发布版本没有问题。

import UIKit

class BaseViewController: UIViewController, UITextFieldDelegate 
var activeField:UITextField?

@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() 

    super.viewDidLoad()




override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.


override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) 
    self.view.endEditing(true);


func textFieldShouldReturn(field: UITextField) -> Bool

    field.resignFirstResponder()

    return true


func textFieldDidBeginEditing(sender: UITextField)

    self.activeField = sender


func textFieldDidEndEditing(sender: UITextField)

    self.activeField = nil


func keyboardDidShow(notification: NSNotification)

    if(self.activeField != nil && self.scrollView != nil) 
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() 
            let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)

            self.scrollView!.contentInset = contentInsets
            self.scrollView!.scrollIndicatorInsets = contentInsets

            var aRect = self.view.frame
            aRect.size.height -= keyboardSize.size.height

            if(!CGRectContainsPoint(aRect, self.activeField!.frame.origin)) 
                self.scrollView!.scrollRectToVisible(self.activeField!.frame, animated: true)
            
                    
    


func keyboardWillBeHidden(notification: NSNotification)

    if(self.scrollView != nil) 
        let contentInsets:UIEdgeInsets = UIEdgeInsetsZero

        self.scrollView!.contentInset = contentInsets
        self.scrollView!.scrollIndicatorInsets = contentInsets
    


我的日志现在看起来像这样,我不确定我的问题是什么。

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed: 
0   libsystem_c.dylib               0x0000000193c19690 __sfvwrite + 0
1   libsystem_c.dylib               0x0000000193c20958 __vfprintf + 11352
2   libsystem_c.dylib               0x0000000193c3b67c __v2printf + 584
3   libsystem_c.dylib               0x0000000193bce208 _vsnprintf + 300
4   libsystem_c.dylib               0x0000000193bcec54 snprintf_l + 20
5   CoreFoundation                  0x0000000181cc7018 __CFStringAppendFormatCore + 11580
6   CoreFoundation                  0x0000000181cc42a0 _CFStringCreateWithFormatAndArgumentsAux2 + 244
7   Foundation                      0x0000000182b0a444 -[NSPlaceholderString initWithFormat:locale:arguments:] + 168
8   Foundation                      0x0000000182b0a304 +[NSString stringWithFormat:] + 72
9   yazda-ios                       0x00000001000fcea0 +[NRMAThreadLocalStore currentThreadDictionary] (NRMAThreadLocalStore.m:238)
10  yazda-ios                       0x00000001000fb678 +[NRMAThreadLocalStore threadLocalTrace] (NRMAThreadLocalStore.m:36)
11  yazda-ios                       0x00000001000fc040 +[NRMAThreadLocalStore prepareSameThread:child:withParent:] (NRMAThreadLocalStore.m:126)
12  yazda-ios                       0x00000001000fbac0 +[NRMAThreadLocalStore pushChild:forParent:] (NRMAThreadLocalStore.m:91)
13  yazda-ios                       0x00000001000c92d4 +[NRMATraceController newTraceSetup:parentTrace:] (NRMATraceController.m:310)
14  yazda-ios                       0x00000001000c97d8 +[NRMATraceController enterMethod:fromObject:parentTrace:traceCategory:withTimer:] (NRMATraceController.m:374)
15  yazda-ios                       0x00000001000c95ac +[NRMATraceController enterMethod:fromObject:parentTrace:traceCategory:] (NRMATraceController.m:338)
16  yazda-ios                       0x00000001000b4498 NRMA__beginMethod (NRMAMethodProfiler.m:1051)
17  yazda-ios                       0x00000001000b406c NRMA__voidParamHandler (NRMAMethodProfiler.m:705)
18  yazda-ios                       0x0000000100047f38 yazda_ios.BaseViewController.viewDidLoad (yazda_ios.BaseViewController)() -> () (BaseViewController.swift:32)
19  yazda-ios                       0x00000001000b407c NRMA__voidParamHandler (NRMAMethodProfiler.m:707)
20  yazda-ios                       0x0000000100047f38 yazda_ios.BaseViewController.viewDidLoad (yazda_ios.BaseViewController)() -> () (BaseViewController.swift:32)

【问题讨论】:

对于您以编程方式添加约束的视图,您是否将 translatesAutoresizingMaskToConstraints 设置为 NO? 我目前不在。我需要在父视图、滚动视图还是我的内容视图上设置它? 【参考方案1】:

我发现了我的问题。出于某种原因,我遇到了依赖问题或构建问题,我的 ViewController 继承了我的 BaseViewController。

由于某种原因,其中一种方法没有像预期的那样返回,它把一切都搞砸了。

【讨论】:

【参考方案2】:

两个约束相互矛盾,所以你可能会面临这样的情况

【讨论】:

以上是关于自动布局约束在生产中引发异常的主要内容,如果未能解决你的问题,请参考以下文章

自动布局 - 以编程方式定义异常约束

在日志中定位自动布局约束错误

自动布局在 centerX-centerY 上不起作用

使用自动布局的 UIToolBar 布局约束问题

自动旋转时自动布局约束更新

自动布局约束错误