How to change resize imageview in titleview - SWIFT 3 - ios

I would like to load an image to the titleview and then change the size.
I'm using this code :
let logo = UIImage(named: "namelogo")
let imageView = UIImageView(frame: CGRect(x:0, y:0, width:70, height:30))
imageView.image = logo
self.navigationItem.titleView = imageView
But this is not working properly. I can play with the height, but the width is not changing !!!

Related

How to fill background image in subview in swift 4?

I have used scrollview. Over the Scrollview I have a UIView where I can set up my image as a background. But problem is that image height is not merged with UIView height. It is shown when scrolling bottom. I want add image with whole UIView.
Here is my image screen shot
Here is my code
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.clipsToBounds = true
backgroundImage.image = UIImage(named: "background_image")
backgroundImage.contentMode = .scaleToFill
self.my_view_2.insertSubview(backgroundImage, at:0)
Please Help me
Thanks
Height of your UIImageView is same as screen size, if your view is greater than screen size, then white view will remain at bottom.
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
Update frame of backgroundImage with scroll's view, as:
let backgroundImage = UIImageView(frame: self.my_view_2.bounds)
backgroundImage.autoresizingMask = [.flexibleHeight, .flexibleWidth] // In case if your my_view_2 frames increases
self.my_view_2.insertSubview(backgroundImage, at: 0)
Add this code in your viewDidLoad(). May be helps you to solve.
if #available(iOS 11.0, *) {
scrollView.contentInsetAdjustmentBehavior = .never
} else {
automaticallyAdjustsScrollViewInsets = false
}
try this
let backgroundImage = UIImageView(frame: my_view_2.bounds)
backgroundImage.clipsToBounds = true
backgroundImage.image = UIImage(named: "background_image")
backgroundImage.contentMode = .scaleToFill
self.my_view_2.addSubview(backgroundImage)
Hope it may help
self.my_view_2.layer.contents = #imageLiteral(resourceName: "background_image");

Setting content mode in UIImageView no longer works in iOS 11

I have this code in order to put an UIImageView in the center of a navigation controller bar and properly scale the image:
override func viewDidAppear(_ animated: Bool) {
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 150, height: 28));
imageView.contentMode = .scaleAspectFit;
let image = UIImage(named: "image_title.png");
imageView.image = image;
self.navigationItem.titleView = imageView;
}
The code works fine in iOS 10, however in iOS 11 the ".scaleAspectFit" property is not considered and the images is not scaled in the UIImageView size.
I tried with some solutions i found:
Setting the frame of the UIImageView after setting the "contentMode" property
Setting imageView.setNeedsLayout()
Setting imageView.setNeedsDisplay()
unfortunately, no one of these solutions works. The "contentMode" property is simply ignored,
Any idea on what the problem could be?
Thank you in advance
Works for me such way (using additional UIView)
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 120, height: 40))
let image = UIImageView(image: UIImage(named: "img-logo"))
image.contentMode = .scaleAspectFit
image.frame = titleView.bounds
titleView.addSubview(image)
viewController.navigationItem.titleView = titleView
let imageView = UIImageView(image: UIImage(named: "img-logo"))
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView

UIImageView not positioning correctly in Navbar and Status Bar

I am trying to use an Image in my Navigation Bar. Keep getting close but no cigar. With the code below, it loads the image fine and positions it nearly correct. It appears to have a pad around the view, but I have no constraints around it. When I remove the Status bar it shrinks the image but positions it fine within the NavBar.
let nav = self.navigationController?.navigationBar
for parent in self.navigationController!.navigationBar.subviews {
for childView in parent.subviews {
if(childView is UIImageView) {
childView.removeFromSuperview()
}
}
}
nav?.barStyle = UIBarStyle.Default
nav?.tintColor = UIColor.blueColor()
let app = UIApplication.sharedApplication()
let SBheight = app.statusBarFrame.size.height
let totalheight = SBheight + nav!.frame.height
let totalwidth = nav!.frame.width
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: totalwidth , height: totalheight))
imageView.clipsToBounds = true
imageView.contentMode = .ScaleAspectFill
var image = UIImage(named: "HRC")
imageView.image = image
navigationItem.titleView = imageView
Here is the shot of the NavBar Area:
I can set the background picture of a navigation bar with no issue with the code below.
Swift 3.0
let backgroundImage = UIImage(named: "name_of_background_image")?.resizableImage(withCapInsets: UIEdgeInsetsMake(0, 0, 0, 0), resizingMode: .stretch)
self.navigationController?.navigationBar.setBackgroundImage(backgroundImage, for: .default)
Swift 2.3
let backgroundImage = UIImage(named: "name_of_background_image")?.resizableImageWithCapInsets(UIEdgeInsetsMake(0, 0, 0, 0), resizingMode: .Stretch)
self.navigationController?.navigationBar.setBackgroundImage(backgroundImage, forBarMetrics: .Default)
You can add the code above to viewDidLoad, and customize the background image for each viewController.
Make sure you render the image as Original from the asset catalog.
And double check if your image has the right size.
#2x -> 750 x 128
#3x -> 1125 x 192

Custom navigation bar image and text

I need in my app a custom navigation bar with an image and a text but I can't add the text.
Here is the code to add the image, how can I add the title?
let logo = #imageLiteral(resourceName: "navigationbaricon")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
Thanks
Where is the frame assigned for self.navigationItem.titleView? Set the frame for imageView and it will work.
You can wrap the UIImageView and the UILabel (which will hold the custom title) in an UIView and then assign the UIView to the self.navigationItem.titleView. Something like this:
let view = UIView(...);
let label = UILabel(...);
label.text = "Custom Title";
let image = UIImageView(image: UIImage(named: "..."));
view.addSubview(image);
view.addSubview(label);
self.navigationItem.titleView = view;
This one is worked for me
override func viewDidLoad() {
super.viewDidLoad()
let titleView = UIView()
titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
titleView.backgroundColor = UIColor.redColor()
let imageView = UIImageView(image: UIImage(named: "img")!)
imageView.frame = CGRectMake(0, 0, 40, 40)
imageView.contentMode = .ScaleAspectFill
titleView.addSubview(imageView)
self.navigationItem.titleView = titleView
}

How can you change the navigation bar title to an image? Other methods have not worked

I have tried the code below and the answer to multiple other questions on Stackoverflow and I still cannot get this to work.. I know the image Profile exists in my assets folder. I have made outlets from my navigation bar and navigation item and tried using that but still got nothing. At most the text I have on the title will disappear...I feel as though I am missing something all together.
Located in ViewController's ViewDidLoad function
let image = UIImage(named: "Profile")
let imageView = UIImageView(image: image)
self.navigationItem.titleView = imageView
self.navigationItem.titleView?.sizeToFit()
Thanks for your help!
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
In Objective C:
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:image];
where "image" is the instance of the image that you want to set as title view.

Resources