[英]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
After making a button, I wanted to move it little bit to the right. 按下按钮后,我想将其向右移动一点。 So, i made UI View to move the button inside the view. 因此,我将UI View设置为在视图内移动按钮。 But, then, after this, the button does not work anymore. 但是,此后,该按钮不再起作用。 Can anyone tell me the solution? 谁能告诉我解决方案?
You're doing a lot of unnecessary things here. 您在这里做了很多不必要的事情。 Firstly, you don't need to put your Button in a container view to put it as the right bar button item. 首先,您无需将Button放在容器视图中即可将其作为右侧的bar按钮项。
You also don't need to add constraints to your searchbutton, since you have given it a fixed frame. 您也不需要在您的搜索按钮上添加约束,因为您已为其指定了固定的框架。
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)
Also as Pratik's comment said, you're meant to use lower camel case. 同样,如Pratik的评论所述,您将使用小骆驼案。 so SearchView should be searchView and SearchButton as searchButton 所以SearchView应该是searchView,而SearchButton应该是searchButton
As for moving it to the right, it seems like that isn't possible anymore without either subclassing the UINavigationBar and changing the implementation, or by making UIViews look exactly like the standard Navigation Bar. 至于将其向右移动,如果没有子类化UINavigationBar并更改实现,或者通过使UIView看起来完全像标准的导航栏,似乎已经不可能了。
Get rid of the space on the right side of a UINavigationBar 摆脱UINavigationBar右侧的空间
Your code is working fine but need to clarify some points. 您的代码工作正常,但需要澄清一些要点。
Reason Your button not working is below 4 lines 原因您的按钮不起作用在4行以下
let WidthConstraint = Searchbutton.widthAnchor.constraint(equalToConstant: 30)
let HeightConstraint = Searchbutton.heightAnchor.constraint(equalToConstant: 30)
WidthConstraint.isActive = true
HeightConstraint.isActive = true
As you already set UIButton
frame then no need to use of above lines of code. 由于已经设置了UIButton
框架,因此无需使用以上代码行。
Yellow color view is your SearchView
and green color is your UIButton
. 黄色是您的SearchView
,绿色是您的UIButton
。
If you use Searchbutton
frame like Searchbutton.frame = CGRect(x: -17, y: -20, width: 30, height: 30)
then it happens something like below image. 如果您使用像Searchbutton.frame = CGRect(x: -17, y: -20, width: 30, height: 30)
类的Searchbutton
框架Searchbutton.frame = CGRect(x: -17, y: -20, width: 30, height: 30)
则会发生以下图像。
You added UIButton
as subview of UIView
and when you click on green button which is inside yellow View will work but the area which is outside Yellow area doesn't work. 您添加了UIButton
作为UIView
子视图,当您单击位于黄色视图内的绿色按钮时可以使用,但位于黄色区域外的区域不起作用。
You need to start UIButton
frame from 0,0,30,30
您需要从0,0,30,30
开始UIButton
框架
Searchbutton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
or you can directly set UIButton
frame same as UIView
frame then it looks like below image. 或者您可以直接将UIButton
框架设置为与UIView
框架相同,则如下图所示。
Searchbutton.frame = SearchView.frame
Below is your Working Code. 以下是您的工作代码。
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.