Logo in Navigation Bar Swift not showing - swift

I'm trying to add a logo to my page's navigation bar, however it doesn't appear when I added this code to my viewDidLoad():
let logo = UIImage(named: "Dog_Log_Icon_H_W")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
Does anybody know why this might not be working? I checked that the image name is correct. This is my storyboard if it helps:

So it worked when I made an outlet,
#IBOutlet weak var navItem: UINavigationItem!
then put the following code in my viewDidAppear:
let logo = UIImage(named: "Dog_Log_Icon_4"){
let imageView = UIImageView(image:logo)
imageView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
navItem.titleView = imageView
not sure why I had to do that..

The reason is because you forgot set the position for the imageView.
You can change your code like this:
let logo = UIImage(named: "Dog_Log_Icon_H_W")
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30)
imageView.image = logo
self.navigationItem.titleView = imageView

You have not set the frame of the image view. I have set frame of the image view and it works fine.
if let logo = UIImage(named: "download"){
let imageView = UIImageView(image:logo)
imageView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
self.navigationItem.titleView = imageView
}else{
print("Image not found...")
}

Related

UIImage not resizing according to UIImageView size width and height

I am a Swift newbie and I am trying to add an UIImageView on the right of UITextField using Storyboard and #IBInspectable annotation.
However, when I define my UIImageView with specific size and add the UIImage, the UIImage is not resizing according to UIImageView size.
Here's my code:
#IBInspectable var rightImage: UIImage? {
didSet {
guard let image = rightImage else {
return
}
rightViewMode = .always
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imageView.image = image
imageView.contentMode = .scaleAspectFit
rightView = imageView
and here's the result on the screen:
Big big icon
If you can tell me what is wrong with my code, do not hesitate.
Thanks.
I use constraints based on Matt’s suggestion in the comment, and it's working now:
let constraints = [
imageView.heightAnchor.constraint(equalToConstant: 20),
imageView.widthAnchor.constraint(equalToConstant: 20)
]
NSLayoutConstraint.activate(constraints)

Navigation Bar logo title not centered

I've implemented a logo as the navigation bar title, using the code below:
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 150, height: 40))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "nav_bar_icon_gradient.png")
imageView.image = image
self.navigationItem.titleView = imageView
It works however the item is fully centred on the navigation bar (see screenshot), I'm aware it could be because of the navigation bar item on the right. Does anyone know how to fix this?

How do I add a background to a scroll view and have it repeat to the end?

I am making a roster on a scroll view with every slide being someone different. I was wondering if there is a way to set a background and have it repeat to the end as it cuts off at a certain person
Currently, I am copy and pasting the background and dragging it to match
for i in 0..
let label = UILabel(frame: CGRect(x:xPosition+110, y: 350, width: 200, height: 200))
label.text = names[i]
label.font = UIFont.systemFont(ofSize: 35, weight: UIFont.Weight.bold)
label.adjustsFontSizeToFitWidth = true
label.textColor = UIColor.black
let imageView = UIImageView()
imageView.image = UIImage(named: names[i])
imageView.contentMode = .scaleAspectFit
imageView.frame = CGRect(x: xPosition-110, y: 150, width: 650, height: 250)
let descriptionM = UITextView(frame: CGRect(x: xPosition+65, y: 525, width: 300, height: 250))
descriptionM.text = desc[i]
descriptionM.textColor = UIColor.black
descriptionM.backgroundColor = UIColor.clear
descriptionM.contentMode = .scaleAspectFill
descriptionM.font = UIFont.systemFont(ofSize: 20)
scrollView.contentSize.width = scrollView.frame.width * CGFloat(i + 1)
scrollView.addSubview(label)
scrollView.addSubview(imageView)
scrollView.addSubview(descriptionM)
}
Results would be the photo to repeat to the end and past.
One way is to turn the background image into a resizable image with zero edge insets and the default (tiling) resizing mode. Set your image view's contentMode to .scaleToFill.
let image = UIImage(named: "background")!.resizableImage(withCapInsets: .zero)
imageView.contentMode = .scaleToFill
imageView.image = image
Then make sure your imageView is at least as big as your scroll view's contentSize.

Set image just one time on NavigationBar Swift

I have an app with several ViewControllers, and I have to display an image in the title of the navigation bar, I already have this code to do it.
public func carregarLogoNav() -> Void {
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 86, height: 38))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "logo-simples.png")
imageView.image = image
navigationItem.titleView = imageView
}
But I would like to know if it is possible to replicate this image to all other views, without having to paste this code in each one.
Another way would be to just call this function in another ViewController, but it is not working either
anotherViewController().carregarLogoNav()
If you wish to have the same titleView in all of your view controllers then you can put your carregarLogoNav function in an extension and then call it from viewDidLoad of each of your view controllers:
extension UIViewController {
public func setupCarregarLogoNav() {
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 86, height: 38))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "logo-simples.png")
imageView.image = image
navigationItem.titleView = imageView
}
}
Then in each view controller:
override func viewDidLoad() {
super.viewDidLoad()
setupCarregarLogoNav()
// any other code
}
This way your setup code is only in once place. It's just called from other places.

Swift UITextField icon position set

Add Icon in UITextField using
var leftImageView = UIImageView()
leftImageView.image = leftImage
textField.leftView = leftImageView
textField.leftViewMode = UITextFieldViewMode.Always
leftImageView.frame = CGRectMake(15, 10, 15, 20)
textField.addSubview(leftImageView)
o/p for this
I found solution like remove this code from above code
textField.leftView = leftImageView
It give icon alignment proper but whenever start editing text field text on icon like this
I want o/p like this
First of all, you should definitely not add the image as a subview to the label. It's enough to set the leftView property.
textField.addSubview(leftImageView) // Delete this line
Secondly, any x or y offsets that you apply to the left view's frame are ignored. The text field will only care about the view's size. If you want to add padding around the image, one option is to use a container view and position the image view inside of it.
let leftImageView = UIImageView()
leftImageView.image = leftImage
let leftView = UIView()
leftView.addSubview(leftImageView)
leftView.frame = CGRectMake(0, 0, 30, 20)
leftImageView.frame = CGRectMake(0, 0, 15, 20)
textField.leftView = leftView
Another option would be to subclass UITextField and override leftViewRectForBounds.
Referring #hennes syntax , there are some syntax missing in swift like CGRect syntax is changed and userNameTextField.leftViewMode = .always is missing
With swift syntax this worked for me :
let leftImageView = UIImageView()
leftImageView.image = UIImage(named: "email")
let leftView = UIView()
leftView.addSubview(leftImageView)
leftView.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
leftImageView.frame = CGRect(x: 10, y: 10, width: 20, height: 20)
userNameTextField.leftViewMode = .always
userNameTextField.leftView = leftView
Try this. May be help you.
var padding: Float = 20
var envelopeView: UIImageView = UIImageView(frame: CGRectMake(padding, 0, 30, 30))
envelopeView.image = UIImage.imageNamed("comment-128.png")
envelopeView.contentMode = UIViewContentModeScaleAspectFit
var viewLeft: UIView = UIView(frame: CGRectMake(padding, 0, 30, 30))
viewLeft.addSubview(envelopeView)
textField.leftView.setFrame(envelopeView.frame)
textField.leftView = viewLeft
textField.leftViewMode = UITextFieldViewModeAlways
var viewRight: UIView = UIView(frame: CGRectMake(textField.frame.size.width - (textField.frame.size.width + 30 + padding), 0, 30, 30))
viewRight.addSubview(envelopeView)
textField.rightView.setFrame(envelopeView.frame)
textField.rightView = viewRight
textField.rightViewMode = UITextFieldViewModeAlways
Swift 3.1
extension UITextField
{
enum Direction
{
case Left
case Right
}
func AddImage(direction:Direction,imageName:String,Frame:CGRect,backgroundColor:UIColor)
{
let View = UIView(frame: Frame)
View.backgroundColor = backgroundColor
let imageView = UIImageView(frame: Frame)
imageView.contentMode = .center
imageView.image = UIImage(named: imageName)
View.addSubview(imageView)
if Direction.Left == direction
{
self.leftViewMode = .always
self.leftView = View
}
else
{
self.rightViewMode = .always
self.rightView = View
}
}
}