我正在尝试向导航项添加三个按钮,我写了这些代码:
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")
}
但是当我点击导航项中的按钮时,什么都不打印。 谁能告诉我原因?谢谢你们。
答案 0 :(得分:1)
您必须将frame
的{{1}}设置为包含3个按钮的大小。否则它的大小为零,并且不会将触摸事件传递给其子视图,因为它们出现在视图之外。
因此,如果您的按钮尺寸为20 x 20并且相互接触,则可以使用:
customNavigationItemView
答案 1 :(得分:0)
我想也许我找到了答案,我改变了我的代码:
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)