执行-layoutSubviews

时间:2015-06-13 14:29:35

标签: ios objective-c swift uitableview

编辑:此问题与Assertion failure in -[UITableView layoutSublayersOfLayer:]不重复。

我有一个包含UITableView的UIViewController。 tableView在顶部包含一个UIView和一个图像。在iOS8上运行以下代码工作正常,但在iOS7I上得到错误:

*** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2935.137/UIView.m:8794
2015-06-13 16:27:47.597 ParallaxTest2[81511:607] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.'
*** First throw call stack:
(
    0   CoreFoundation                      0x002b91e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x01a6d8e5 objc_exception_throw + 44
    2   CoreFoundation                      0x002b9048 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x007214de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x00bc2a38 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 567
    5   libobjc.A.dylib                     0x01a7f82b -[NSObject performSelector:withObject:] + 70
    6   QuartzCore                          0x0497c45a -[CALayer layoutSublayers] + 148
    7   QuartzCore                          0x04970244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    8   QuartzCore                          0x0497c3a5 -[CALayer layoutIfNeeded] + 160
    9   UIKit                               0x00c84ae3 -[UIViewController window:setupWithInterfaceOrientation:] + 304
    10  UIKit                               0x00b9aaa7 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 5212
    11  UIKit                               0x00b99646 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
    12  UIKit                               0x00b99518 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117
    13  UIKit                               0x00b995a0 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67
    14  UIKit                               0x00b9863a __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120
    15  UIKit                               0x00b9859c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400
    16  UIKit                               0x00b992f3 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870
    17  UIKit                               0x00b9c8e6 -[UIWindow setDelegate:] + 449
    18  UIKit                               0x00c76b77 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180
    19  UIKit                               0x00b92474 -[UIWindow addRootViewControllerViewIfPossible] + 591
    20  UIKit                               0x00b925ef -[UIWindow _setHidden:forced:] + 312
    21  UIKit                               0x00b9286b -[UIWindow _orderFrontWithoutMakingKey] + 49
    22  UIKit                               0x0c345587 -[UIWindowAccessibility(SafeCategory) _orderFrontWithoutMakingKey] + 77
    23  UIKit                               0x00b9d3c8 -[UIWindow makeKeyAndVisible] + 65
    24  UIKit                               0x00b4dbc0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 2097
    25  UIKit                               0x00b52667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    26  UIKit                               0x00b66f92 -[UIApplication handleEvent:withNewEvent:] + 3517
    27  UIKit                               0x00b67555 -[UIApplication sendEvent:] + 85
    28  UIKit                               0x00b54250 _UIApplicationHandleEvent + 683
    29  GraphicsServices                    0x03bf2f02 _PurpleEventCallback + 776
    30  GraphicsServices                    0x03bf2a0d PurpleEventCallback + 46
    31  CoreFoundation                      0x00234ca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    32  CoreFoundation                      0x002349db __CFRunLoopDoSource1 + 523
    33  CoreFoundation                      0x0025f68c __CFRunLoopRun + 2156
    34  CoreFoundation                      0x0025e9d3 CFRunLoopRunSpecific + 467
    35  CoreFoundation                      0x0025e7eb CFRunLoopRunInMode + 123
    36  UIKit                               0x00b51d9c -[UIApplication _run] + 840
    37  UIKit                               0x00b53f9b UIApplicationMain + 1225
    38  ParallaxTest2                       0x00111274 main + 180
    39  libdyld.dylib                       0x022f46d9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

这是我的UIViewController的viewDidLoad()方法:

  override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = UIColor.yellowColor()
    self.navigationController?.navigationBar.mg_setBackgroundColor(UIColor.clearColor())

    tableView = UITableView()
    tableView.dataSource = self
    tableView.delegate = self
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellIndentifier)

    self.view.addSubview(tableView)

    tableView.snp_makeConstraints { (make) -> Void in
      make.edges.equalTo(self.view)
    }

    topView = UIView()

    topView.backgroundColor = UIColor.yellowColor()
    topView.contentMode = UIViewContentMode.ScaleToFill

    let tableHeaderDummyView = UIView(frame: CGRectMake(0, 0, 0, headerHeight))
    tableHeaderDummyView.backgroundColor = UIColor.clearColor()
    tableView.tableHeaderView = tableHeaderDummyView

    tableHeaderDummyView.snp_makeConstraints { (make) -> Void in
      make.top.equalTo(tableView.snp_top)
      make.height.equalTo(headerHeight)
      make.left.equalTo(self.view.snp_left)
      make.right.equalTo(self.view.snp_right)
    }


    let img = UIImage(named: "bg")
    topImageView = UIImageView(image: img)
    topImageView.contentMode = UIViewContentMode.ScaleAspectFit
    topView.addSubview(topImageView!)

    topImageView.snp_makeConstraints { (make) -> Void in
      make.edges.equalTo(topView)
    }


    topImageView?.contentMode = UIViewContentMode.ScaleToFill

    tableView.addSubview(topView!)
    tableView.sendSubviewToBack(topView!)


    topView.snp_makeConstraints { (make) -> Void in
      make.top.equalTo(tableView.snp_top).offset(-40)
      make.left.equalTo(self.view.snp_left)
      make.right.equalTo(self.view.snp_right)
      make.height.equalTo(200)
    }

  }

如何阻止iOS7抛出此错误?

0 个答案:

没有答案