How to add image to top layout - ios

How to add background image on top layout the one who has back button . ive tried adding imageview on it using the drag and drop but image is hiding on the back. Anyone has an idea? Thanks

Add background image on top layout the one who has back button
Its called NavigationBar. You can set background image by this:
self.navigationController.navigationBar.setBackgroundImage(UIImage(named: "navBG.png"), for: .default)
Edit:
how about adding a label on a navigation bar?
self.title = "My Title"

Swift 4
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "Background.jpg"), for: .default)

Related

Center title on the button image

I want to change button image and title programmatically. When design button image and title (plain) on storyboard it is centered no problem. But when ı use;
startButton.setImage(UIImage(named: "StartButton"), for: .normal)
startButton.setAttributedTitle(NSAttributedString(string: "START"), for: .normal)
to change button image and title programmatically. Button title not centered on image. It looks like it's on the left of the image.
This may helps you.
use setBackgroundImage instead of setImage
startButton.setBackgroundImage(UIImage(named: "StartButton"), for: .normal)
hope this solution will solve your problem 😊

Insert Icon in Navigation Bar / make BarButtonItem not clickable Swift iOS

I would like to place a bluetooth icon in my Navigation Bar, to display a connected / disconnected status.
I tried to add a BarButtonItem, set the image as my bluetooth icon, and disabled and enabled this Button. This works fine so far, and is looking ok to me, but I don't want to have this button clickable, so that it doesn't change the color on clicking on the icon.
Is this possible, or is there a way to put a UIImageView into the Navigation Bar?
Thanks!
Try disabling touch events using:
myBarButtonItem.isUserInteractionEnabled = false
navigationItem.rightBarButtonItem?.isEnabled = false
Add following code will fix your issue.
let btnBluetooth = UIButton()
btnBluetooth.setImage(#imageLiteral(resourceName: "icon_bluetooth"), for: .normal)
btnBluetooth.setImage(#imageLiteral(resourceName: "icon_bluetooth"), for: .highlighted)
btnBluetooth.tintColor = .red
let barButton = UIBarButtonItem(customView: btnBluetooth)
self.navigationItem.rightBarButtonItem = barButton
This code will add custom button where you can manage image for normal and highlight mode. For bluetooth state management, change tintColor of UIButton which you have added in UIBarButtonItem as custom view.
If you click on button, this will not change color of image.
If you don't want to add UIButton you can add UIImageView by following code.
let imgBluetooth = UIImageView(image: #imageLiteral(resourceName: "icon_bluetooth"))
imgBluetooth.tintColor = .red
let barButton = UIBarButtonItem(customView: imgBluetooth)
self.navigationItem.rightBarButtonItem = barButton
Also, make sure you have selected Render As as Template Image for your bluetooth icon which is added inside Assets.xcassets to affect tintColor. Otherwise image will be display as original. See follow:

Navigation Bar and Status Bar colors/ invisible iOS

I'm trying to realize navigation bar and status bar that take colors of the image on top of controller. I've try with this two code:
self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.shadowImage = UIImage()
but it change only the main controller and I need to make invisible only the second controller not the main. Here is an image of what I want exactly.
If you want the color of the image just put the image on top and later a visual effect view.
You can make the navigation bar transparent with this extension by calling:
navigationController?.navigationBar.apply(.transparentWhite)
in the viewWillAppear of any view controller in which you want this behaviour.
If you want other themes, define them as I defined the one in the gist:
static var transparentWhite: NavigationTheme { return NavigationTheme(attributes: [.font: UIFont(name: .avenirNextRegular, size:14.0)], barColor: .clear, tintColor: .white) }`

Xcode transparent navigation bar background

I'm using a custom navigation bar in a view, and I want the bar not to be visible (except for the text and the buttons of course). the view is white, therefore the background of the bar needs to be also white or transparent. but no matter, what I try, it is always barely visible:
This is what it looks like(I know you have to look hard, but the bottom line is visible):
if I need to write any code, please use swift
Try this :
self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true

UINavigation bar not getting cleared in swift3 Xcode

I am trying to achieve a transparent navigation bar such that the background image is shown clearly. Currently i have used a base controller class where i have put a code for transparent navigation bar:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = .clear
But currently i am getting the clear navigation bar only on first page. If i push and go to the second controller i can see a empty white space.
See the below images:
First page with cleared navigation bar
Second page with empty white space at the top
Why the navigation bar is not getting transparent? Any ideas?
As Your navigation bar is transparent, it will show your viewcontroller's view background color.
I think, your second page view controller's view background color is White. Thats the reason you are getting white color.
Add Top Constraint to SuperView, Not to TopLayout Guide
change the view background
self.navigationController?.navigationBar.isTranslucent = true
For more detail about isTranslucent read this doc
https://developer.apple.com/documentation/uikit/uinavigationbar/1624928-translucent

Resources