How is it possible to add icons to my UItextfield? - swift4

I would very much appreciate the help :), and also is there any way to make the underline change colors when entering a username or password?

Try adding below code:
let iconWidth = 20;
let iconHeight = 20;
//Define the imageView
let imageView = UIImageView();
let imageEmail = UIImage(named: "xyz.png");
imageView.image = imageEmail;
// set frame on image before adding it to the uitextfield
imageView.frame = CGRect(x: 5, y: 5, width: iconWidth, height: iconHeight)
textField.leftViewMode = UITextFieldViewMode.Always
textField.leftView = imageView

You can use UIImageView and this too as a subview inside the UITextField

Related

How can I insert image to end of the string and insert more than one image in UITextView with swift?

I created an UITextView and I can add image with image picker into text view. But I have some problem about replacement image. I want add this image end of the text. And I want to add images more than one. (like: text + image + text...). How can I solve this problem ? Can anyone help me ?
let pickImage = UIImageView() // this is for imagepickercontroller
lazy var writePost: UITextView = {
let wpost = UITextView()
let images = pickImage
let attachment = NSTextAttachment()
let attString = NSAttributedString(attachment: attachment)
attachment.image = images.image
images.frame = CGRect(x: 0, y: 0, width: 220, height: 220)
images.contentMode = .scaleAspectFill
wpost.textStorage.insert(attString, at: wpost.selectedRange.location)
wpost.addSubview(images)
wpost.textAlignment = .center
wpost.textColor = UIColor.lightGray
wpost.font = UIFont(name: "AvenirNext-DemiBoldItalic", size: 16)
wpost.isEditable = true
wpost.isScrollEnabled = true
wpost.layer.borderWidth = 1.5
wpost.layer.cornerRadius = 7.0
wpost.layer.borderColor = UIColor.orange.cgColor
wpost.delegate = self
return wpost
}()
What you should do is use UITextView's textContainer's exclusionPaths property. The exclusionPaths property lets you assign an array of UIBezierPaths to your textContainer. When exclusionPaths are set, none of the UITextView's text will appear within these paths. You could then add a UIImageView as a subview of the UITextView's super view placed above the UITextView that has a frame equal to said exclusion path.
The end result will be a UITextView with a UIImageView placed above it. None of the UITextView's text will be blocked by the UIImageView as the UITextView's textContainer's exclusionPaths have instructed the text not to populate there.
Here is an example of some code I've done to do something similar, with variable names changed to match your code a bit:
let imageView: UIImageView!
func addImageView() {
imageView = UIImageView(frame: CGRect(x: textView.frame.maxX - 200, y: textView.frame.maxY - 150, width: 200, height: 150))
textView.superView.addSubview(imageView)
}
func setExclusionPath(for imageView: UIImageView) {
let imageViewPath = UIBezierPath(rect: CGRect(x: textView.frame.maxX - imageView.frame.width, y: textView.frame.maxY - imageView.frame.height, width: imageView.frame.width, height: imageView.frame.height))
textView.textContainer.exclusionPaths.append(imageViewPath)
}
func someMethod() {
addImageView()
setExclusionPath(for: self.imageView)
}
Resources:
exclusionPaths reference from Apple

Swift - TableView background image: how to center the image

I want to add a logo as a background image to my tableView. Image size is 50px, 50px.
I tried the code below, but this puts the image lower right corner.
let imageView = UIImageView(image: UIImage(named: "logo"))
imageView.contentMode = .scaleAspectFit
imageView.layer.frame = CGRect(x: self.view.frame.midX, y: self.view.frame.midY, width: 50, height: 50)
let tableViewBackgroundView = UIView()
tableViewBackgroundView.addSubview(imageView)
self.tableView.backgroundView = tableViewBackgroundView
There are a few points about swift that are pretty key:
1) The x and y parameters in CGRect.init(x:y:width:height:) don't refer to the center of the imageView. Instead, they are points in a coordinate system where (0, 0) is the Upper Left Corner of the view that the CGRect is being presented over, and
2) UIImageView actually inherits from UIView, so because of this you can just say
tableView.backgroundView = imageView
You shouldn't have to fiddle around with any CGRects, I believe this code should work just fine, although you may have to fiddle with different contentMode properties to get it to display how you like.
let imageView = UIImageView(image: UIImage(named: "logo"))
imageView.layer.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
imageView.layer.frame.midX = tableView.layer.frame.midX
imageView.layer.frame.midY = tableView.layer.frame.midY
tableView.backgroundView = imageView

How to change the size of the titleView of Navigation Item? (Swift)

In my app I want to change the titleView of my Navigation Controller to a custom ImageView but I have a very strange problem. I basically just can`t set the frame of my ImageView.
This is my code:
func NavBarEinrichten(){
let logo = UIImage(named: "Notennamen für Griffe 1")!
let imageView = UIImageView(image:logo)
imageView.contentMode = .scaleAspectFit
//To show the problem better
imageView.backgroundColor = UIColor.red
let navController = navigationController!
let bannerWidth:CGFloat = 20
let bannerHeight:CGFloat = navController.navigationBar.frame.size.height
let bannerX = bannerWidth / 2 - logo.size.width / 2
let bannerY = bannerHeight / 2 - logo.size.height / 2
imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
self.navigationItem.titleView?.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
navigationItem.titleView = imageView
print(imageView.frame, "imageView frame")
}
The print statement prints:
(0.0, 0.0, 357.0, 142.0) imageView frame
Which is right, because it looks like this when i run it, but not like it should be. Because I want the width to be 20.
In the simulator it looks like this:
I tried to call this method in viewDidLoad(), viewWillAppear()and viewDidLayoutSubviews()
Every time with the exact same outcome...
Can you help me?

Can't fit UIImage inside UIScrollview

I'm using this code to pick an image from gallery and fit it inside a UIScrollView:
let imageView = UIImageView(image: inputImageDelegate!)
imageView.frame = CGRect(x: 0, y: 0, width: contentView.frame.width, height: contentView.frame.height)
imageView.contentMode = UIViewContentMode.ScaleAspectFill
inputImage = imageView
contentView.addSubview(imageView)
self.scrollView.minimumZoomScale = 0.5
self.scrollView.maximumZoomScale = 6.0
self.scrollView.contentSize = self.inputImage.frame.size
self.scrollView.delegate = self
self.scrollView.zoomScale = 1
imageView.sizeToFit()
Result:
I want to make image's width equal to the screen width after loading (user can scale it up and down). Neither of self.scrollView.zoomScale = 1 nor imageView.sizeToFit() worked.
I'll assume you have your scroll view connected to an IBOutlet like this:
#IBOutlet weak var scrollView: UIScrollView!
You should set the frame of the UIImageView based on the UIScrollView's contentSize property.
let imageView = UIImageView(image: inputImageDelegate!)
let contentSize = self.scrollView.contentSize // convenience
imageView.frame = CGRect(x: 0, y: 0, width: contentSize.width, height: contentSize.height)
...

Swift add image or icon in UITextField [duplicate]

This question already has answers here:
Swift add icon/image in UITextField
(21 answers)
Closed 5 years ago.
I would like to add icon/image in UITextfield .The Icon/Image should be left to placeholder.I tried this coding but it is not showing icon/image inside the textfield .
var imageView = UIImageView();
var image = UIImage(named: "usericon.png");
imageView.image = image;
username.leftView = imageView;
username.leftViewMode = UITextFieldViewMode.Always
I think you forgot to add this code:
imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
view.addSubview(imageView)
Image will show if u use this code.
May be this will help you.
And In order to leave a space between the icon and the text
let imageView = UIImageView();
let image = UIImage(named: "icon_calendar");
imageView.image = image;
imageView.frame = CGRect(x: 10, y: 10, width: 20, height: 20)
textfield.addSubview(imageView)
let leftView = UIView.init(frame: CGRectMake(10, 0, 30, 30))
textfield.leftView = leftView;
textfield.leftViewMode = UITextFieldViewMode.Always