Xcode 7.3.1 : Set background image to UINavigationBar and display back button - ios

I want to set logo of app as background image to UINavigationBar and when user traverse into app it should display logo as well as back button on top of it.
Below is code that I've used :
func setNavigationBar() {
let navigationBarHeight: CGFloat = self.navigationController!.navigationBar.frame.height
let screenSize: CGRect = UIScreen.mainScreen().bounds
let objCustomView = CustomView(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: navigationBarHeight))
let objWindow = UIApplication.sharedApplication().keyWindow
objWindow?.addSubview(objCustomView)
self.navigationItem.setHidesBackButton(false, animated:true);
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.Plain, target:nil, action:nil)
}
The issue with this is that back button goes behind the image.
How to fix this?
After refering post by #NDoc I'm getting extra space in left. Why so?
Also, the back button should be white with no back text i.e. only < arrow.
Below is code for customView :
class CustomView: UIView {
var imgLogo = UIImageView(frame:CGRectZero)
override init(frame: CGRect) {
super.init(frame: frame)
let screenSize: CGRect = UIScreen.mainScreen().bounds
imgLogo.frame = CGRectMake(0, 0, screenSize.width, 44.0)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
imgLogo.image = UIImage(named:"BoM_Logo")
self.addSubview(imgLogo)
}
}

You can display your logo in leftBarButtonItem and set the leftItemsSupplementBackButton to true to display backButton also like this.
let logoView = UIImageView(frame: CGRect(x: 0, y: 0, width: 60, height: 30))
logoView.image = UIImage(named: "Logo")
let item = UIBarButtonItem(customView: logoView)
self.navigationItem.leftBarButtonItem = item
To show the back button with your logo image set leftItemsSupplementBackButton to true
self.navigationItem.leftItemsSupplementBackButton = true
Edit:
If you want custom arrow then you need to use leftBarButtonItems and pass array of BarButtonItem and no need to set leftItemsSupplementBackButton to true like this.
let logoView = UIImageView(frame: CGRect(x: 0, y: 0, width: 60, height: 30))
logoView.image = UIImage(named: "Logo")
let logoItem = UIBarButtonItem(customView: logoView)
let btnBack = UIButton(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
btnBack.setImage(UIImage(named: "Back_Arrow"), forState: .Normal)
btnBack.addTarget(self, action: #selector(self.buttonAction(_:)), forControlEvents: .TouchUpInside)
let backItem = UIBarButtonItem(customView: btnBack)
self.navigationItem.leftBarButtonItems = [backItem, logoItem]
Note: Don't forgot to add buttonAction action method inside your viewController.

Try this in your appDelegate
let image = UIImage.init(named:"upper-bar.png")
UINavigationBar.appearance().setBackgroundImage(image,forBarMetrics:UIBarMetrics.Default)
for back button try this in the viewDidLoad() of your viewController
let image1 = UIImage(named: "go10.png") as UIImage?
let btnLeft = UIButton(type: .Custom)
btnLeft.frame = CGRectMake(0, 0, 25, 25)
btnLeft.setImage(image1,forState:UIControlState.Normal)
btnLeft.addTarget(self, action:(#selector(NameofyourViewController.backBtn(_:))),forControlEvents:UIControlEvents.TouchUpInside)
let leftBarButton = UIBarButtonItem(customView: btnLeft)
self.navigationItem.leftBarButtonItem = leftBarButton
#IBAction func backBtn(sender: UIButton)
{
self.navigationController?.popViewControllerAnimated(true)
}

Related

Adjust position of custom Navigation Back Button

I am using a custom image as the back button of my Navigation Controller but the problem is that the image is not aligned correctly with the title and the right button item. I've been trying to move the back button down a few pixels with no success.
extension UINavigationController {
func addBackButton() {
let imgBack = UIImage(named: "ic_back")
navigationBar.backIndicatorImage = imgBack
navigationBar.backIndicatorTransitionMaskImage = imgBack
navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "",
style: .plain,
target: self,
action: nil)
}
}
This is what it looks like now:
As you can see I need to move the back button down a little, any help would be much appreciated.
This is my code for a custom back button. I added on viewDidLoad() with the image below. Maybe you need to test with your image how to size it correctly. And you can remove the part with the label.
let backButtonView = UIView(frame: CGRect(x: 0, y: 0, width: 60, height: 44))
let imageView = UIImageView(image: UIImage(named: "back-arrow"))
imageView.frame = CGRect(x: -5, y: 11, width: 12, height: 22)
imageView.image = imageView.image!.withRenderingMode(.alwaysTemplate)
imageView.tintColor = .blue
let label = UILabel(frame: CGRect(x: 10, y: 0, width: 40, height: 44))
label.textColor = .blue
label.text = "Back"
backButtonView.addSubview(imageView)
backButtonView.addSubview(label)
backButtonView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(backButtonClicked)))
let barButton = UIBarButtonItem(customView: backButtonView)
navigationItem.leftBarButtonItem = barButton

How to fit an image in rightbarbuttonitem

I've been trying to put a hamburger button (the three parallel lines) to the right of the titleView in the nav bar, but every time I do, the image I put in covers the entire nav bar and gets rid of the image I have in titleView.
If I select a default image in the storyboard editor it will appear on the right side of the nav bar without any problems, but as soon as I select the hamburger button in the storyboard editor I get the same problem as before. I've tried with multiple different images and I've changed up the code a little bit with no success. Is there a way to resize the image I'm using so it will fit in the nav bar properly or is there just something wrong with my code?
Here is my code from viewController.swift below:
override func viewDidAppear(_ animated: Bool) {
let nav = self.navigationController?.navigationBar
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageView.contentMode = .scaleAspectFit
let titleImage = UIImage(named: "logowhitecircle")
imageView.image = titleImage
navigationItem.titleView = imageView
let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
menuButton.contentMode = .scaleAspectFit
let menuImage = UIImage(named: "hamburgericon")
menuButton.setImage(menuImage, for: .normal)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: menuButton)
You need to set image size in the image assets like 3x = 84px, 2x=56px, 1x = 28px,
see the apple document for more info: https://developer.apple.com/design/human-interface-guidelines/ios/icons-and-images/custom-icons/
let menuButton = UIBarButtonItem(image: UIImage(named: "logowhitecircle"), style: .plain, target: self, action: #selector(menuButtonTapped(_:)))
self.navigationItem.rightBarButtonItem = menuBu
tton
Try Using below code:
let imageBurger = UIImage(named: "hamburgericon")!
let btnLeftMenu = UIButton(type: .system)
btnLeftMenu.bounds = CGRect(x: 10, y: 0, width: imageBurger.size.width, height: imageBurger.size.height)
btnLeftMenu.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
btnLeftMenu.setImage(imageBurger, for: UIControl.State())
btnLeftMenu.setTitle(title, for: .normal)
let leftButton = UIBarButtonItem(customView: btnLeftMenu)
self.navigationItem.leftBarButtonItem = leftButton
Try this module FFBadgedBarButtonItem it's easy to use module, here is the documenataion Link
Below is my code how to implement it!
let image = UIImage(named: "yourImage")
let finalImage = resizeImage(image: image!, newWidth: 30)
navigationItem.rightBarButtonItem = FFBadgedBarButtonItem(image: finalImage, target: self, action: #selector(rightButtonTouched))
And here is the calling function
#objc func rightButtonTouched() {
// what event you need to perfom by clicking on this button
}
You need to create Bridging Header to work with this Obj-C module.
: D
Try this:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//create a new button
let button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
//set image for button
button.setImage(UIImage(named: "hamburgericon"), forState: UIControlState.Normal)
//add function for button
button.addTarget(self, action: "customButtonPressed", forControlEvents: UIControlEvents.TouchUpInside)
//set frame
button.frame = CGRectMake(0, 0, 40, 40)
let barButton = UIBarButtonItem(customView: button)
//assign button to navigationbar
self.navigationItem.rightBarButtonItem = barButton
}
//This method will call when you press button.
func customButtonPressed() {
println("button pressed")
}
}

Adding image in the Navigation Bar

I was wondering the best approach to put an image into the navigation bar.
My initial thought was to create a cocoa touch class for UINavigationController and set it up that way, but I can seem to get it to working using the below code:
class NavBarImage: UINavigationController {
override func awakeFromNib() {
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
self.setupView()
}
func setupView()
{
let navController = navigationController!
let image = #imageLiteral(resourceName: "BarTabsNavLogoWhite")
let imageView = UIImageView(image: image)
let bannerWidth = navController.navigationBar.frame.size.width
let bannerHeight = navController.navigationBar.frame.size.height
let bannerX = bannerWidth / 2 - image.size.width / 2
let bannerY = bannerHeight / 2 - image.size.height / 2
imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth,
height: bannerHeight)
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView
}
}
I keep getting an "unexpectedly found nil while unwrapping an optional value" on let navController = navigationController!.
However, this method has also been working for me too. I created a cocoa touch class for UINavigationBar and used this code below:
import UIKit
class NavBarImg: UINavigationBar {
override init(frame: CGRect) {
super.init(frame: frame)
initialise()
}
required init(coder aDecoder: NSCoder){
super.init(coder: aDecoder)!
initialise()
}
func initialise(){
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 225, height: 40))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named:"BarTabsNavLogoWhite")
imageView.image = image
imageView.frame.origin = CGPoint(x: self.superview?.center.x, y: self.superview?.center.y)
addSubview(imageView)
}
}
The only problem with this is that on different iPhones I cant figure out how to get the image to always be centered on any device using CGPoint.
Then for the last method I found and implemented is done by the code below:
#IBDesignable class test: UINavigationBar { #IBInspectable var imageTitle: UIImage? = nil {
didSet {
guard let imageTitle = imageTitle else {
topItem?.titleView = nil
return
}
let imageView = UIImageView(image: imageTitle)
imageView.frame = CGRect(x: 0, y: 0, width: 40, height: 30)
imageView.contentMode = .scaleAspectFit
topItem?.titleView = imageView
}
}
}
I really like this method because with the IBDesignable function you can see it in the storyboard. However the way I have my viewcontrollers set up with tableviews, after i go past the first view controller, the navigation bar image disappears in all other view controllers when I run the simulator.
Looking for advice to see which method is the best approach and how to possibly solve the problems I am having. Or if anyone has a different method that they have found that works, id love to see how it works!
you can simply add a image or customize the barbutton as follows:
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "icon_right"), for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
button.frame = CGRect(x: 0, y: 0, width: 53, height: 31)
button.imageEdgeInsets = UIEdgeInsetsMake(-1, 32, 1, -32)//move image to the right
let label = UILabel(frame: CGRect(x: 3, y: 5, width: 20, height: 20))
label.font = UIFont(name: "Arial-BoldMT", size: 16)
label.text = "title"
label.textAlignment = .center
label.textColor = .black
label.backgroundColor = .clear
button.addSubview(label)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.rightBarButtonItem = barButton

RightBarButtonItems won't be shown anymore on iOS 11

The function self.navigationItem.rightBarButtonItem = cartBarButton won't show my item on the right of UINavigationController's NavBar
EDIT
let cartOriginalImage = UIImage.cart
let cartButton = UIImageButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
cartButton.setBackgroundImage(cartOriginalImage, for: .normal)
cartButton.tintColor = Colors.Navigation.Tint.default
cartButton.addTarget(self, action: #selector(MainViewController.cartButtonPressedInHomePage(sender:)), for: .touchUpInside)
let cartBarButton = UIBarButtonItem(customView: cartButton)
if let badge = badge {
cartBarButton.badgeValue = badge
cartBarButton.badge.backgroundColor = Colors.Navigation.Background.badge
cartBarButton.badge.textColor = Colors.Navigation.Text.badge
}
I had the similar issue with custom image view. My suggestion is to wrap your custom view into container view.
private func configureUserAvatarView() {
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(avatarViewAction(_:)))
let customView = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
customView.addGestureRecognizer(tapRecognizer)
let imageView = FUIImageView(image: #imageLiteral(resourceName: "avatar_default_30х30"))
imageView.isCircular = true
imageView.contentMode = .scaleAspectFit
imageView.frame = customView.bounds
customView.addSubview(imageView)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: customView)
}

UIButton inside a UIView not responding to gesture

I have a UIView with a UIButton inside it but the button does not recognise gestures
Any ideas why? I have tried using the solutions in the questions related to this but have not succeeded.
The whole class code:
import UIKit
import Foundation
public class HUDView: UIView , UIGestureRecognizerDelegate {
var stopwatch: StopwatchView
var gamePoints: CounterLabelView
var hintButton: UIButton!
required public init(coder aDecoder: NSCoder) {
fatalError("Never call this... Use init(frame:)")
}
override init(frame: CGRect) {
self.stopwatch = StopwatchView(frame: CGRect(x: ScreenWidth/2 - 150, y: 0, width: 300, height: 100))
self.stopwatch.setSecondsRemaining(0)
self.gamePoints = CounterLabelView(font: FontHUD!, frame: CGRect(x: ScreenWidth - 200, y: 30, width: 320, height: 70))
gamePoints.textColor = UIColor.black
gamePoints.value = 0
super.init(frame: frame)
self.addSubview(gamePoints)
let pointsLabel = UILabel(frame: CGRect(x: ScreenWidth - 340, y: 30, width: 140, height: 70))
pointsLabel.backgroundColor = UIColor.clear
pointsLabel.font = FontHUD
pointsLabel.text = " Points:"
self.addSubview(pointsLabel)
self.isUserInteractionEnabled = false
self.addSubview(self.stopwatch)
//load the button image
let hintButtonImage = UIImage(named: "btn")!
//the help button
self.hintButton = UIButton()
// hintButton.perform(#selector(HUDView.s))
hintButton.setTitle("Hint!", for:.normal)
hintButton.titleLabel?.font = lHud
hintButton.setBackgroundImage(hintButtonImage, for: .normal)
hintButton.frame = CGRect(x: 50, y: 30, width: hintButtonImage.size.width, height: hintButtonImage.size.height)
// hintButton.center = self.center
//50, 30, hintButtonImage.size.width, hintButtonImage.size.height
hintButton.alpha = 1.0
hintButton.isUserInteractionEnabled = true
self.addSubview(hintButton)
// hintButton.addTarget(self, action: #selector(self.tapButton(_:)), for: .touchUpInside)
let g = UITapGestureRecognizer(target: self, action: #selector(self.h(_:)))
g.delegate = self
hintButton.addGestureRecognizer(g)
}
func h(_ sender: UITapGestureRecognizer) {
print("hey!")
fatalError()
}
}
The problem is in line self.isUserInteractionEnabled = false
You are adding button as a subview to HUDView. The button tap will not work because the parent view ie:HUDView has user interaction disabled and so the user interaction of all the subViews will be disabled.
Make the changes as self.isUserInteractionEnabled = true it will work.

Resources