Customizing image size in code in iOS navigation controller - ios

This is the code I'm using to set an image in the title of the nativation bar,
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "fullLogo")
imageView.image = image
self.navigationItem.titleView = imageView;
The issue is that image appears a little too big. How do I resize the image in code? I tried many combinations of the width and height but it doesn't seem to work.

According to documentation:
Custom title views are centered on the navigation bar and may be resized to fit.
That's what happening with your image view. The solution can be to add imageView as subview to UIView and assign UIView to titleView property.
The easier solution may be to set imageView.contentMode to .center but in this case you need to be sure that your fullLogo image is actually 100x30, if it is bigger than titleView frame calculated by iOS your image will overlay navigation bar and view controller's view.
And you can init your UIImageView or container UIView with frame: .zero because it's frame will be recalculated in runtime.

Set
imageView.clipsToBounds = true
For resizing the image this SO Post would help. Hope this helps.
Happy Coding.

Related

UICollectionView.backgroundView broken

I have a problem with backgroundView property. I tried to add an imageView to background like this
let backgroundImage = UIImage(named: "Fridge_background")!
let backgroundView = UIImageView(image: backgroundImage)
collectionView?.backgroundView = backgroundView
but the problem is that it did not even add this view to view hierarchy. I have already read this article, but this solution did not help me either. What it can be and how did I can fix it? Funny think is that in tableView similar property work just fine as it must.
Broken it is indeed.
I was researching improper positioning of uicollectionview background view
and run into this:
https://blog.spacemanlabs.com/2013/11/uicollectionviews-backgroundview-property-is-horribly-broken/
describes your issue as well.
This seemed to work well in the UIColllectionViewController's viewDidAppear method. All behaviors matched expectations.
let myView = UIView(frame: self.view.frame)
let image = UIImage(named: "image name")
let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: image!.size))
imageView.image = image
myView.addSubview(imageView)
self.collectionView.backgroundView = myView

centering titleView in navigationItem

I am trying to center my titleView in my navigationItem.
After localizing my app, I saw this behavior:
This is my code as an extension to navigationItem:
extension UINavigationItem{
func makeImg(){
let container = UIView(frame: CGRect(x: 0,y: 0,width: 200,height: 40))
let logo = UIImage(named: "Rookie")
let imageView = UIImageView(frame: CGRect(x: 66.75, y: 7.25, width: 66.5, height: 25.5))
imageView.image = logo
imageView.layer.masksToBounds = true
imageView.clipsToBounds = true
container.addSubview(imageView)
self.titleView = container
}
}
I think it has something to do with my superView in navigationItem.
So I should reference the Screens bounds. Any ideas?
This happens if the view set as titleView is very wide. Views that are added as a titleView are only resized if their width is wider than the available space, in which case they're resized to fill the space between the left bar button item and the right.
In this case, you've set your container object as being 200 points wide, which is wider than the available space with both of those items.
If you set the container width to match the image view (i.e., 66.5 points), then it should become centered. :)
(Or conversely, if you don't plan to add any other views, you can set the image view directly as the titleView)

How to center an image inside an UIButton without stretching in both direction in Swift?

I am making a custom keyboard and I need to centre the image of the shift icon inside the shift button. The image is 100px x 100px.
However, the button frame in portrait is 36pt x 38pt, and in landscape, it is 68pt x 32pt. I have tried using button image inset property to centre the image. However, it does not does so.
I tried myself and set the image edge insets to (25,25,25,25), the image centres in both sizes. However, the icon becomes too small to see.
shiftButton.setImage(UIImage(named: "shift"), forState: .Normal)
shiftButton.imageEdgeInsets = UIEdgeInsetsMake(0,0,0,0)
I am making the button programatically. And, I need the button to scale maintaining its aspect ratio i.e. 1:1. Its maximum width or height is always tied to the button's height itself. The image width/height should be maximum of button's height. And, it should not stretch in any way to distort the icon. However, the button itself can stretch.
I need the image to scale according to button frame but maintain its aspect ratio. And, it can down scale or up scale, but should always maintain the 1:1 aspect ratio.
Any suggestions for solving this problem?
Finally a solution came through. You must set the content mode for the image inside the UIButton.
The solution is to update the contentMode of the Image when you are using a foreground image inside a button along with the contentMode of the UIButton.
shiftButton.contentMode = .Center
shiftButton.imageView?.contentMode = .ScaleAspectFit
If you're using storyboard, double-check that you don't have anything set for the button title.
Add User Defined Runtime Attributes to your button.
In the screen shot below value 1 is equal to scaleAspectFit
imageView.ContentMode = scaleAspectFit
I was able to center the image by setting the content insets to 0 using the size inspector on the storyboard:
Set the button's content mode to Center:
shiftButton.contentMode = .Center
To work on UIButton with image is sort of tricky things. I think below two lines could solve your problem.
And there is a good post Aligning text and image on UIButton
// Keep button's image to its original ratio.
button.imageView?.contentMode = .scaleAspectFit
// Give button image a little space from all the four edges.
button.imageEdgeInsets = UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3)
A button in my project
lazy var downloadButton: TransitionButton = {
let button = TransitionButton()
button.translatesAutoresizingMaskIntoConstraints = false
let config = UIImage.SymbolConfiguration(pointSize: 26, weight: .bold, scale: .default)
let image = UIImage(systemName: "arrow.down", withConfiguration: config)?.withTintColor(.white, renderingMode: .alwaysOriginal)
button.setImage(image, for: .normal)
button.imageView?.contentMode = .scaleAspectFit
button.imageEdgeInsets = UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3)
button.backgroundColor = .systemPink
button.layer.cornerRadius = 12
button.addTarget(self, action: #selector(downloadButtonTapped(_:)), for: .touchUpInside)
button.spinnerColor = .white
return button
}()

Putting imageview on navigation bar

I want to add image to right side of navigation bar. But I can't drag image view to navigation bar. I can only drag a button.
How can I do this ? I want same thing with whatsapp profile image. You know they have circle profile picture on right side of navigation bar.
Drag a UIView and drop it on the navigation bar first. Then drag a UIImageView into the view.
That should give you what you are looking for.
Set the image view height and width using constraints. You have to set the width and height of the containing view in the size inspector.
**This will Display the image on right side on navigation bar**
func viewDidLoad(){
let containView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
let imageview = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageview.image = UIImage(named: "photo.jpg")
imageview.contentMode = UIViewContentMode.scaleAspectFit
imageview.layer.cornerRadius = 20
imageview.layer.masksToBounds = true
containView.addSubview(imageview)
let rightBarButton = UIBarButtonItem(customView: containView)
self.navigationItem.rightBarButtonItem = rightBarButton
}
Don't know about storyboards. But from code you can create UIBarButtonItem with custom view.
let customView = UIImageView()
let customViewItem = UIBarButtonItem(customView: customView)
self.navigationItem.rightBarButtonItem = customViewItem
From storyboard:
Just drag UIBarButtonItem into your controller navigation bar. In element menu(Attribute inspector) select identifier: Custom and Image: the image you want.

UI Navigation Bar Image Title

How can I set the title of a UINavigationBar to be an image using Swift.
For example, Instagram does the following:
Objective: Find how to set the title of a UINavigationBar to be an image using Swift.
You can do the following :
var titleView = UIImageView(image: UIImage(named: "nameOfTheImage.ext"))
self.navigationItem.titleView = titleView
To fits the the image in the dimension you want you have to do the follwoing :
var titleView : UIImageView
// set the dimensions you want here
titleView = UIImageView(frame:CGRectMake(0, 0, 50, 70))
// Set how do you want to maintain the aspect
titleView.contentMode = .ScaleAspectFit
titleView.image = UIImage(named: "nameOfTheImage.ext")
self.navigationItem.titleView = titleView

Resources