Swift:iOS在为UISearchBar设置动画时,没有为约束准备视图层次结构

时间:2015-09-03 11:08:34

标签: swift autolayout

我已经对"视图层次结构进行了大量研究......"问题,我相信我已经尝试过在SO上提供的每一次解决方案。我认为我的问题必须有点不同(或者我错过了一些明显的东西)因为在searchBarTextDidBeginEditing期间动画期间更新约束失败了

我正在尝试将UISearchBar直接添加到主UIView。我将inactiveSearchBarConstraintY设置为全局变量,因为我稍后会对其进行修改。我用以下内容实现了这一点(在viewDidLoad和viewDidAppear中没有区别):

    override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.dataSource = self
    self.tableView.delegate = self

    self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()
        controller.searchBar.frame.size.width = self.view.frame.size.width
        controller.searchBar.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.view.addSubview(controller.searchBar)

        let searchBarConstraintX = NSLayoutConstraint(item: controller.searchBar, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0)
        self.view.addConstraint(searchBarConstraintX)

        self.inactiveSearchBarConstraintY = NSLayoutConstraint(item: controller.searchBar, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 300)
        self.view.addConstraint(self.inactiveSearchBarConstraintY)

        let searchBarConstraintWidth = NSLayoutConstraint(item: controller.searchBar, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 300)
        self.view.addConstraint(searchBarConstraintWidth)

        let searchBarConstraintHeight = NSLayoutConstraint(item: controller.searchBar, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100)
        self.view.addConstraint(searchBarConstraintHeight)
        controller.searchBar.delegate = self 

        return controller
    })()
像我说的那样,这很好。搜索栏已添加到self.view并位于inactiveSearchBarConstraintY

指示的距离

当我尝试编辑搜索栏时出现问题。当我触摸搜索栏时,它和桌面视图都应该向上移动(不同数量,这就是为什么它们没有附加)。

    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    self.resultSearchController.active = true;

    self.inactiveSearchBarConstraintY.constant = 0 //<-- crashes with or without this
    self.tableViewConstraintY.constant = 200

    UIView.animateWithDuration(0.7, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: {
        self.view.layoutIfNeeded()
        }, completion: { finished in
            self.resultSearchController.active = true
    })
}

使用此代码,当我触摸搜索栏时应用程序崩溃。

实际上,应用程序在功能中没有任何内容崩溃!如下:

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
self.resultSearchController.active = true;
}

这是失败消息

The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x78f9dd40 UISearchBar:0x78e10a80.leading == UIView:0x78e39380.leading>

When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.

2015-09-03 07:03:27.108 MyApp[24606:415328] *** Assertion failure in -[UIView _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:], /SourceCache/UIKit_Sim/UIKit-3347.44/NSLayoutConstraint_UIKitAdditions.m:560

0 个答案:

没有答案