Can I set image as a title to UINavigationBar?
You can use an UIImageView
for the UINavigationItem.titleView
property, something like:
self.navigationItem.titleView = myImageView;
How to Use Image as title on large Navigation Bar?
Well, you can already place the image in the bar, so now it's just a matter of resizing the view so it fits to your desired position.
Do this by resizing the frame of the UIImage
, as resizing the UIImageView
alone doesn't work in the navigation bar (see here).
Edit: After some experimentation, I managed to do what you want. It's a bit hack-y and you might need to figure out the correct size values for yourself, but I managed to achieve this view:
This was done by adding a UIView
titled "container" as the titleView
, which then contained a UIImageView
. While "container" is mostly restricted to the "top" part of the bar, the contained UIImageView
is able to move freely inside the Navigation Bar now. You can see how this is a bit hack-y:
In this image you can see that the container UIView
has a black background color. It can't grow past a certain size as the titleView
. Inspecting the view hierarchy shows you how the logo is positioned:
This was achieved through the following code:
let logo = UIImage(named: "logo")
let container = UIView(frame: CGRect(x: 0, y: 0, width: 182, height: 132))
container.backgroundColor = UIColor.clear
let imageView = UIImageView(frame: CGRect(x: -132, y: 2, width: 182, height: 132))
imageView.contentMode = .scaleAspectFit
imageView.image = logo
container.addSubview(imageView)
self.navigationItem.titleView = container
As I said, this is hack-y and certainly not intended by Apple. This solution might not work in future versions of iOS.
You can also set width
and height
of container
to 0
and experiment from there. It's all about manually finding the right frame values through trial and error.
How to put an image as the navigation bar title
Set the navigationItem's titleView
UIImage *image = [UIImage imageNamed:@"image.png"];
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:image];
Navigation bar with UIImage for title
Put it inside an UIImageView
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
Objective-C: Background image and title in Navigation Bar
I got it!
@implementation UINavigationBar(MyNavigationBar)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"navBarBackgrd.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
See Background image for navigation view
How can you change the navigation bar title to an image? Other methods have not worked
initially set the frame for imageview
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageView.contentMode = .ScaleAspectFit
if let image = UIImage(named: "Profile")
{
imageView.image = image
}else
{
imageView.image = UIImage(named: "Profile.png")
}
self.navigationItem.titleView = imageView
UINavigationBar - prefersLargeTitles - How to set image to the left of the title?
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let img = UIImage.init(named: "imgName")
let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
imgView.image = img!
imgView.contentMode = .scaleAspectFit
let item = UIBarButtonItem.init(customView: imgView)
let negativeSpacer = UIBarButtonItem.init(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
navigationItem.leftBarButtonItems = [negativeSpacer, item]
}
}
The result
If you want to execute a request when you click the image, replace this line
let negativeSpacer = UIBarButtonItem.init(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
with that line
let negativeSpacer = UIBarButtonItem.init(barButtonSystemItem: .fixedSpace, target: self, action: #selector(go))
And place this
@objc func go() {
print("go")
}
Related Topics
Nsstring Encoding Returns Nil on Url Content
Nsdictionaryofvariablebindings Swift Equivalent
How to Remove All Gesture Recognizers from a Uiview in Swift
How to Get the Status Bar Height in iOS 13
Swift: Get All Subviews of a Specific Type and Add to an Array
Avaudiosession Setcategory Swift 4.2 iOS 12 - Play Sound on Silent
Can't Set Titleview in the Center of Navigation Bar Because Back Button
How to Convert String to Date to String in Swift iOS
How to Add Done Button on Keyboard on Top of Keyboard in iOS
App Transport Security Does Not Work Any More with iOS 11 Xcode 9
How to Use Device Instead of Simulator
How to Upload Audio with Alamofire Multipart Upload
Adding Multiple Arrays to Form One Final Array. Debug Swift Xcode
No Such File and Directory Found Xcode 7
Return Lighter Color from Skcolor Using Hsl Lightness Factor
Adjust Font Size of Text to Fit in Uibutton
How to Get Weekday And/Or Name of Month from a Nsdate Variable
"File Not Found", "Linker Command Failed with Exit Code 1" in Xcode 4.5.1