3

I'm trying to override programmatically the rightButton of a navigation item with a static image in order to achive this:

enter image description here

The following code does not give any errors but nothing is displayed in the navigation bar.

import UIKit
import Foundation
class UICipNavigationController: UINavigationController {

override func viewDidLoad() {
    super.viewDidLoad()


    // Add BPT LOGO as UIBarButton
    let logoBPT = UIImage(named: "Logo BPT")?.withRenderingMode(.alwaysOriginal)

    let logoBPTBarButton = UIBarButtonItem(image: logoBPT, style: .plain, target: nil, action: nil)
    self.navigationItem.rightBarButtonItem = logoBPTBarButton


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

.

import UIKit

class UICipNavigationBar: UINavigationBar {

override init(frame: CGRect){
    super.init(frame: frame)

}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!

    self.tintColor = UIColor.white

    self.setBackgroundImage(UIImage.fromColor(color: UIColorFromHex(rgbValue: 0xffffff, alpha: 0.2)), for: UIBarMetrics.default)
    self.shadowImage = UIImage()
    self.isTranslucent = true
    self.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName: UIFont(name: "Lato-Regular", size: 24)!]

}

override func sizeThatFits(_ size: CGSize) -> CGSize {
    let newSize :CGSize = CGSize(width: self.superview!.bounds.size.width, height: 60)
    return newSize
}

}

I also tried to inspect the views but it seems that nothing is added:

enter image description here

Alessio Dal Bianco
  • 930
  • 1
  • 11
  • 20
  • try this .. may be works for you http://stackoverflow.com/questions/30022780/uibarbuttonitem-in-navigation-bar-programmatically/30022852#30022852 – Bhavin Bhadani Nov 24 '16 at 08:36
  • Just tried, but i had no luck. The code always override the storyboard right? – Alessio Dal Bianco Nov 24 '16 at 08:55
  • your code works fine.just tested. may be your image file not exist ?. i dont understand.why is the white space in your image file name `Logo BPT` – Joe Nov 24 '16 at 09:30
  • Because is the name i gave to the image set, it have the space. Unless i need to refer to name of the -image file-. – Alessio Dal Bianco Nov 24 '16 at 09:43
  • check my update..... – Joe Nov 24 '16 at 10:09
  • yes. but I can see you using custom navigationBar with 60px height.i recommend you to embedded a `navigationController` to your VC and try the code or check my answer from the link http://stackoverflow.com/questions/40316352/increase-navigationbar-height/40320225#40320225 and follow the answer from this post and compare with my answer and you dont need to setup `class for navigationBar` you can simply call `var customBar: UINavigationBar = UINavigationBar()` from your mainVC. hope this helps... – Joe Nov 24 '16 at 10:35

4 Answers4

6

Note: Image name/file/extention is sensitive.When you setting a barButtonImage.Make sure you setting them properly.I am assessing image file from asset catalog(32pt).

 let myimage = UIImage(named: "YourImageFileName")?.withRenderingMode(.alwaysOriginal)
 navigationItem.rightBarButtonItem = UIBarButtonItem(image: myimage, style: .plain, target: self, action: #selector(ButtonTapped))


 func ButtonTapped() { 
 print("Button Tapped")  
}

Output: updated

enter image description here

Method 2: Testing Purpose

Note: set your title as a emoji icon from Xcode. run the project and see that works?

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: self, action: #selector(ButtonTapped))

    func ButtonTapped() { 
    print("Button Tapped")  
   }

Output:

enter image description here

Joe
  • 8,868
  • 8
  • 37
  • 59
-1

You should setup the right bar button from within your top view controller that is being displayed by the navigation controller

Bobj-C
  • 5,276
  • 9
  • 47
  • 83
-2
    let button = UIButton.init(type: .custom)
    button.setImage(UIImage.init(named: "menu"), for: UIControlState.normal)
    button.frame = CGRect.init(x: 0, y: 0, width: 25, height: 17)
    let barButton = UIBarButtonItem.init(customView: button)
    self.navigationItem.rightBarButtonItem = barButton
  • 1
    Is the same method of http://stackoverflow.com/questions/40781380/insert-right-button-image-in-uinavigationcontroller-with-swift-3/40781752#comment68785841_40781380 – Alessio Dal Bianco Nov 24 '16 at 09:02
-2

Please refer below code.

let button: UIButton = UIButton (type: UIButtonType.Custom)
button.setImage(UIImage(named: "emojiImage"), forState: UIControlState.Normal)
//button.addTarget(self, action: "emojiImageButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
button.frame = CGRectMake(0, 0, 30, 30)
let barButton = UIBarButtonItem(customView: button)

self.navigationItem.rightBarButtonItem = barButton

Make sure your image has to be plain ( transparent ) background.

If you want click event then uncomment button.addTarget(self, action: "emojiImageButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside) and put below function to call.

func emojiImageButtonPressed(btn : UIButton) {

    print("emojiImageButtonPressed")
}
Hasya
  • 9,792
  • 4
  • 31
  • 46