[英]button does not work after adding uiview in swift 4
let SearchView = UIView()
SearchView.backgroundColor = UIColor.clear
SearchView.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
let Searchbutton = UIButton(type: .system)
Searchbutton.setImage(UIImage (named: "SEARCH"), for: .normal)
Searchbutton.backgroundColor = .clear
Searchbutton.frame = CGRect(x: -17, y: -20, width: 30, height: 30)
Searchbutton.contentMode = .scaleAspectFit
let WidthConstraint = Searchbutton.widthAnchor.constraint(equalToConstant: 30)
let HeightConstraint = Searchbutton.heightAnchor.constraint(equalToConstant: 30)
WidthConstraint.isActive = true
HeightConstraint.isActive = true
let barButtonItem = UIBarButtonItem(customView: SearchView)
self.navigationItem.rightBarButtonItem = barButtonItem
SearchView.addSubview(Searchbutton)
Searchbutton.addTarget(self, action: #selector(viewanewcontroller(button:)), for: .touchUpInside)
//right search button
按下按钮后,我想将其向右移动一点。 因此,我将UI View设置为在视图内移动按钮。 但是,此后,该按钮不再起作用。 谁能告诉我解决方案?
您在这里做了很多不必要的事情。 首先,您无需将Button放在容器视图中即可将其作为右侧的bar按钮项。
您也不需要在您的搜索按钮上添加约束,因为您已为其指定了固定的框架。
let Searchbutton = UIButton(type: .system)
Searchbutton.setImage(UIImage (named: "SEARCH"), for: .normal)
Searchbutton.backgroundColor = .clear
Searchbutton.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
Searchbutton.contentMode = .scaleAspectFit
let barButtonItem = UIBarButtonItem(customView: Searchbutton)
self.navigationItem.rightBarButtonItem = barButtonItem
Searchbutton.addTarget(self, action: #selector(viewanewcontroller(button:)), for: .touchUpInside)
同样,如Pratik的评论所述,您将使用小骆驼案。 所以SearchView应该是searchView,而SearchButton应该是searchButton
至于将其向右移动,如果没有子类化UINavigationBar并更改实现,或者通过使UIView看起来完全像标准的导航栏,似乎已经不可能了。
您的代码工作正常,但需要澄清一些要点。
原因您的按钮不起作用在4行以下
let WidthConstraint = Searchbutton.widthAnchor.constraint(equalToConstant: 30)
let HeightConstraint = Searchbutton.heightAnchor.constraint(equalToConstant: 30)
WidthConstraint.isActive = true
HeightConstraint.isActive = true
由于已经设置了UIButton
框架,因此无需使用以上代码行。
黄色是您的SearchView
,绿色是您的UIButton
。
如果您使用像Searchbutton.frame = CGRect(x: -17, y: -20, width: 30, height: 30)
类的Searchbutton
框架Searchbutton.frame = CGRect(x: -17, y: -20, width: 30, height: 30)
则会发生以下图像。
您添加了UIButton
作为UIView
子视图,当您单击位于黄色视图内的绿色按钮时可以使用,但位于黄色区域外的区域不起作用。
您需要从0,0,30,30
开始UIButton
框架
Searchbutton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
或者您可以直接将UIButton
框架设置为与UIView
框架相同,则如下图所示。
Searchbutton.frame = SearchView.frame
以下是您的工作代码。
let SearchView = UIView()
SearchView.backgroundColor = UIColor.clear
SearchView.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
let Searchbutton = UIButton(type: .system)
Searchbutton.setImage(UIImage (named: "SEARCH"), for: .normal)
Searchbutton.backgroundColor = .clear
Searchbutton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
Searchbutton.contentMode = .scaleAspectFit
let barButtonItem = UIBarButtonItem(customView: SearchView)
self.navigationItem.rightBarButtonItem = barButtonItem
SearchView.addSubview(Searchbutton)
Searchbutton.addTarget(self, action: #selector(viewanewcontroller(button:)), for: .touchUpInside)
//right search button
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.