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设置为在视图内移动按钮。但是,此后,该按钮不再起作用。谁能告诉我解决方案?
答案 0 :(得分:0)
您在这里做了很多不必要的事情。首先,您无需将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看起来完全类似于标准导航栏,似乎再也无法实现了。
答案 1 :(得分:0)
您的代码工作正常,但需要澄清一些要点。
原因您的按钮不起作用在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
之类的Searchbutton.frame = CGRect(x: -17, y: -20, width: 30, height: 30)
框架,则会在图像下方发生类似的事情。
您添加了UIButton
作为UIView
的子视图,当您单击黄色内部的绿色按钮时,“视图”将起作用,但黄色区域之外的区域将不起作用。
您需要从UIButton
开始0,0,30,30
框架
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