ios – 添加UISearchController并以编程方式使用约束对其进行定位

ios – 添加UISearchController并以编程方式使用约束对其进行定位,第1张

概述我想以编程方式将前导,尾随,底部和宽度约束添加到UISearchController.这是我的代码: @IBOutlet weak var navigationBar: UIView!// create search barsearchBar = UISearchController(searchResultsController: nil)navigationBar.addSubview 我想以编程方式将前导,尾随,底部和宽度约束添加到UISearchController.这是我的代码:

@IBOutlet weak var navigationbar: UIVIEw!// create search barsearchbar = UISearchController(searchResultsController: nil)navigationbar.addSubvIEw(searchbar.searchbar)searchbar.searchbar.translatesautoresizingMaskIntoConstraints = falselet leftConstraint = NSLayoutConstraint(item: searchbar.searchbar,attribute: .Leading,relatedBy: .Equal,toItem: navigationbar,multiplIEr: 1,constant: 0)let rightConstraint = NSLayoutConstraint(item: searchbar.searchbar,attribute: .Trailing,constant: 0)let bottomConstraint = NSLayoutConstraint(item: searchbar.searchbar,attribute: .Bottom,constant: 0)let heightConstraint = NSLayoutConstraint(item: searchbar.searchbar,attribute: .Height,toItem: nil,attribute: .NotAnAttribute,constant: 44)navigationbar.addConstraints([leftConstraint,rightConstraint,bottomConstraint,wIDthConstraint])

运行应用程序时,搜索栏会正确显示,但是当我按下搜索栏时,它会缩小,如果我按下另一次,应用程序崩溃了.这是输出:

2015-08-12 20:20:37.696 Contacts++[96997:8547485] The vIEw hIErarchy is not prepared for the constraint: <NSLayoutConstraint:0x7fb22580c7a0 UIVIEw:0x7fb225817b20.leading == UIVIEw:0x7fb223449860.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(UIConstraintBasedLayout) _vIEwHIErarchyUnpreparedForConstraint:] to deBUG.2015-08-12 20:20:37.697 Contacts++[96997:8547485] *** Assertion failure in -[UIVIEw _layoutEngine_dIDAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:],/buildroot/library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3491.2.5/NSLayoutConstraint_UIKitAdditions.m:590
解决方法 为什么需要创建’UISearchController’的实例并获取其searchbar?

为什么不从UISearchbar创建searchbar?

// create a searchbar from UISearchbarlet searchbar = UISearchbar(frame: CGRectZero)// add searchbar to navigationbarnavigationController?.navigationbar.addSubvIEw(searchbar)// call sizetoFit.. this will set the frame of the searchbar to exactly the same as the size of the allowable frame of the navigationbarsearchbar.sizetoFit()// Now reframe the searchbar to add some marginsvar frame = searchbar.frameframe.origin.x = 20frame.size.wIDth -= 40searchbar.frame = frame // set new frame with margins

注意:您不需要任何这些约束来实现此目的.

但是如果你真的更喜欢约束,这里是没有崩溃的约束代码.

let searchbar = UISearchbar(frame: CGRectZero)navigationController?.navigationbar.addSubvIEw(searchbar)searchbar.setTranslatesautoresizingMaskIntoConstraints(false)let leftConstraint = NSLayoutConstraint(item: searchbar,toItem: navigationController?.navigationbar,constant: 20) // add marginlet bottomConstraint = NSLayoutConstraint(item: searchbar,constant: 1)let topConstraint = NSLayoutConstraint(item: searchbar,attribute: .top,constant: 1)let wIDthConstraint = NSLayoutConstraint(item: searchbar,attribute: .WIDth,constant: self.vIEw.frame.size.wIDth - 40) // - margins from both sIDesnavigationController?.navigationbar.addConstraints([leftConstraint,topConstraint,wIDthConstraint])
总结

以上是内存溢出为你收集整理的ios – 添加UISearchController并以编程方式使用约束对其进行定位全部内容,希望文章能够帮你解决ios – 添加UISearchController并以编程方式使用约束对其进行定位所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1072806.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存