【问题标题】:UIButton in navigation item does not respond to click导航项中的 UIButton 不响应单击
【发布时间】:2015-11-19 09:09:30
【问题描述】:

我正在尝试向导航项添加三个按钮,我编写了以下代码:

let customNavigationItemView : UIView = UIView()
let trendbutton : UIButton = UIButton(type: .Custom)
size = myString.sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14.0)])
size.width = size.width * 1.5
nearbybutton.frame.size = size
...
let nearbybutton : UIButton = UIButton(type: .Custom)
...
let squarebutton : UIButton = UIButton(type: .Custom)
...
trendbutton.frame.origin = CGPointMake(-trendbutton.frame.size.width - nearbybutton.frame.size.width / 2, 0)
...
customNavigationItemView.addSubview(trendbutton)
customNavigationItemView.addSubview(nearbybutton)
customNavigationItemView.addSubview(squarebutton)
self.navigationItem.titleView = customNavigationItemView

trendbutton.addTarget(self, action:  "circleNavigationBar_trendButton_clickUp:", forControlEvents: .TouchUpInside)
nearbybutton.addTarget(self, action: "circleNavigationBar_nearbyButton_clickUp:", forControlEvents: .TouchUpInside)
squarebutton.addTarget(self, action: "circleNavigationBar_squareButton_clickUp:", forControlEvents: .TouchUpInside)

还有这个:

func circleNavigationBar_trendButton_clickUp(sender : UIButton) {
    print("trend click")
}

但是当我单击导航项中的按钮时,什么也不打印。 谁能告诉我原因?还是谢谢大家。

【问题讨论】:

    标签: uinavigationcontroller uinavigationbar swift2 uinavigationitem


    【解决方案1】:

    您必须将customNavigationItemViewframe 设置为包含3 个按钮的大小。否则它的大小为零并且不会将触摸事件传递给它的子视图,因为它们发生在视图之外。

    因此,如果您的按钮大小为 20 x 20 并且相互接触,这将起作用:

    let customNavigationItemView : UIView = UIView()
    customNavigationItemView.frame = CGRectMake(0, 0, 60, 20)
    
    let trendbutton : UIButton = UIButton(type: .Custom)
    trendbutton.frame = CGRectMake(0, 0, 20, 20)
    ...
    let nearbybutton : UIButton = UIButton(type: .Custom)
    nearbybutton.frame = CGRectMake(20, 0, 20, 20)
    ...
    let squarebutton : UIButton = UIButton(type: .Custom)
    squarebutton.frame = CGRectMake(40, 0, 20, 20)
    

    【讨论】:

    • 实际上我这样设置框架并更新了问题,但它不起作用。还是谢谢你们。
    【解决方案2】:

    我想也许我找到了答案,我像这样更改了我的代码:

    trendbutton.frame = CGRectMake(0, 0, trendSize.width, trendSize.height)
    nearbybutton.frame = CGRectMake(trendSize.width, 0, nearbySize.width, nearbySize.height)
    squarebutton.frame = CGRectMake(trendSize.width + nearbySize.width, 0, squareSize.width, squareSize.height)
    customNavigationItemView?.frame = CGRectMake(self.circleTable_tableView.frame.width / 2, 0, trendSize.width + nearbySize.width + squareSize.width, max(trendSize.height, nearbySize.height, squareSize.height))
    

    现在效果很好,但我不明白这两种设置框架的方法之间的区别: 一个是:

    someView.frame = CGRectMake(0,0,10,10)
    

    另一个是:

    someView.frame.origin = CGPointMake(0,0) 
    someView.frame.size = CGSizeMake(10,10)
    

    【讨论】:

      猜你喜欢
      • 2020-10-07
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多