迅速4中添加uiview后,按钮不起作用

时间:2018-10-03 11:24:22

标签: ios swift xcode

 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设置为在视图内移动按钮。但是,此后,该按钮不再起作用。谁能告诉我解决方案?

2 个答案:

答案 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看起来完全类似于标准导航栏,似乎再也无法实现了。

Get rid of the space on the right side of a UINavigationBar

答案 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)框架,则会在图像下方发生类似的事情。

enter image description here

您添加了UIButton作为UIView的子视图,当您单击黄色内部的绿色按钮时,“视图”将起作用,但黄色区域之外的区域将不起作用。

您需要从UIButton开始0,0,30,30框架

Searchbutton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)

或者您可以直接将UIButton框架设置为与UIView框架相同,然后如下图所示。

Searchbutton.frame = SearchView.frame

enter image description here

下面是您的工作代码。

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