IOS - Swift - 向BarButtonItem的customView添加目标和操作

时间:2014-11-06 08:18:58

标签: ios iphone uinavigationbar uinavigationitem

我正在创建一个自定义视图,将uiimageview作为子视图。然后我在导航栏中使用该自定义视图作为rightBarButtonItem。这适用于在正确的位置显示正确的图标,但无论出于何种原因,我在“动作”中定义的函数永远不会被调用,因此当我点击rightBarButtonItem时不执行segue。奇怪的是,如果我不插入自定义视图,而是注释掉该位,只需为rightItem设置标题,目标和操作,那么该函数将正确执行。以某种方式添加自定义视图与目标和动作属性混淆,但我似乎无法弄清楚如何解决它。任何帮助将不胜感激!

//create custom view for right item and set it
    var imageViewRight:UIImageView = UIImageView()
    imageViewRight.frame = CGRectMake(5, 10, 35, 25)
    let rightImage:UIImage = UIImage(named: "cameraIconInactive")!
    imageViewRight.image = rightImage
    var rightView:UIView = UIView()
    rightView.frame = CGRectMake(0, 0, 45, 45)
    rightView.addSubview(imageViewRight)
    var rightItem:UIBarButtonItem = UIBarButtonItem()
   //this line right below is the problem - if I comment it out and replace with a simple rightItem.title = "test" and leave everything else the same, then the method runs properly
    rightItem.customView = rightView
    rightItem.target = self
    rightItem.action = "pushProfileToCamera"
    self.navigationItem.rightBarButtonItem = rightItem

}

func pushProfileToCamera(){
    println("pushing profile to camera")
    self.performSegueWithIdentifier("pushProfileToCamera", sender: nil)
}

编辑:

实际上坐了24小时,在我看到这些答案建议之前提出了这个解决方案..任何理由我都不应该这样做?它有效..

//create the custom rightBarButtonItem
    //create the imageView with icon
    var imageViewRight:UIImageView = UIImageView()
    imageViewRight.frame = CGRectMake(5, 10, 35, 25)
    var rightImage:UIImage = UIImage(named: "cameraIconInactive")!
    imageViewRight.image = rightImage
    //put the imageView inside a uiview
    var rightView:UIView = UIView()
    rightView.frame = CGRectMake(0, 0, 45, 45)
    rightView.addSubview(imageViewRight)
    //create the tap gesture recognizer for that uiview
    var rightGestureRecognizer:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "pushProfileToCamera")
    rightView.addGestureRecognizer(rightGestureRecognizer)
    //create the uibarbuttonitem, assign our custom view to it, and insert it in the nav bar!
    var rightItem:UIBarButtonItem = UIBarButtonItem()
    rightItem.customView = rightView
    self.navigationItem.rightBarButtonItem = rightItem

4 个答案:

答案 0 :(得分:6)

您可以尝试代替链接rightItemAction并将UIView添加到customView,将UIButton添加到rightBarButtonItem:

    var button: UIButton = UIButton()
    button.setImage(UIImage(named: "cameraIconInactive"), forState: .Normal)
    button.frame = CGRectMake(0, 0, 45, 45)
    button.targetForAction("pushProfileToCamera", withSender: nil)

    var rightItem:UIBarButtonItem = UIBarButtonItem()
    rightItem.customView = button
    self.navigationItem.rightBarButtonItem = rightItem

答案 1 :(得分:4)

试试这个

var rightItem:UIBarButtonItem = UIBarButtonItem(image: rightImage, landscapeImagePhone: rightImage, style: UIBarButtonItemStyle.Plain, target: self, action: "pushProfileToCamera")

答案 2 :(得分:3)

self.navigationController?.navigationBarHidden =  false

    let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 64)) // Offset by 20 pixels vertically to take the status bar into account

    navigationBar.backgroundColor = UIColor.blueColor()
    navigationBar.delegate = self;

    // Create a navigation item with a title
    let navigationItem = UINavigationItem()

    //menu button
    let menubutton: UIButton = UIButton(frame: CGRectMake(0, 0, 30, 30))
    menubutton.setImage(UIImage(named: "menu"), forState: UIControlState.Normal)
    menubutton.addTarget(self, action: "btn_clicked", forControlEvents: UIControlEvents.TouchUpInside)

    //menu button custom view
    let leftView = UIView(frame: CGRectMake(0,0,30,30))
    leftView.addSubview(menubutton)

    //left uibarbutton
    let leftItem:UIBarButtonItem = UIBarButtonItem(customView: leftView)
    navigationItem.leftBarButtonItem = leftItem


    //searchButton
    let searchbutton: UIButton = UIButton()
    searchbutton.setImage(UIImage(named: "search1x"), forState: UIControlState.Normal)
    searchbutton.frame = CGRectMake(0, 0, 30, 30)
    searchbutton.addTarget(self, action: "btn_clicked", forControlEvents: UIControlEvents.TouchUpInside)


    //menu button custom view
    let rightView = UIView(frame: CGRectMake(0,0,30,30))
    rightView.addSubview(searchbutton)

    //right uibarbutton
    let rightItem:UIBarButtonItem = UIBarButtonItem(customView: rightView)
    navigationItem.rightBarButtonItem = rightItem

    // Assign the navigation item to the navigation bar
    navigationBar.items = [navigationItem]

    // Make the navigation bar a subview of the current view controller
    self.view.addSubview(navigationBar)

答案 3 :(得分:1)

UIBarButtonItem已经有了这个初始值设定项init(image: UIImage?, style: UIBarButtonItemStyle, target: AnyObject?, action: Selector)