iOS - Set ImageView background color and mask - ios

How can I set the background color of an ImageView for an icon and have it mask to bounds? So that the inner transparent parts of the image are then the background color?
Example: in this twitter icon, how can I set the inner white (transparent) parts of the icon to be a different color?
EDIT: What if the image is NOT a circle? But any shape.

You have to follow the following steps
imageView.image = UIImage(named: "yourImageName")?.withRenderingMode(.alwaysOriginal)
imageView.layer.cornerRadius = 14 //number of your choice
imageView.layer.masksToBounds = true
imageView.tintColor = .black

imageView.backgroundColor = .blue //Sets the background color
imageView.layer.cornerRadius = imageView.width/2 //Crops the image to be a circle
imageView.layer.masksToBounds = true //Sets the mask to bounds
If you want a custom shape you can do this:
create two imageViews (above each other)
first imageView use as usual
second imageView use as background shape
Code for the one in foreground
firstImageView.backgroundColor = .clear
Second imageView (in the background)
secondImageView.image = UIImage(named: "yourImage").withRenderingMode(.alwaysTemplate)
secondImageView.tintColor = yourBackgroundColor
secondImageView.backgroundColor = .clear

Related

Empty space in UIImageView when using scaleAspectFit

Here is an image of my issue. My problem is that I have an tiny empty space on the right side of my UIImageView that is constrained to the view of my view controller. It's green because of the background I set, and it is more prominent on my device. It's weird because the image should fit the whole screen considering the image was taken on the phone itself and I am using scaleAspectFit.
let imageView: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.isUserInteractionEnabled = true
imageView.contentMode = .scaleAspectFit
imageView.backgroundColor = .green
imageView.clipsToBounds = true
return imageView
}()
Here is the code for the constraints:
func setupImageView() {
self.view.addSubview(imageView)
let imageViewConstraints = [
imageView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
imageView.topAnchor.constraint(equalTo: self.view.topAnchor),
imageView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
imageView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
]
NSLayoutConstraint.activate(imageViewConstraints)
}
I am basically setting the image using self.imageView.image and I end up with a empty space on the right side that is only noticeable when using bright colors as the imageView background. Not sure if this is a bug. The only reason I'm not using fill is because I want to maintain the aspect while showing the whole image.
The contentMode scaleAspectFit will keep the scale of the image, display the whole content of the image. Because your photo scale is same to the screen, if your imageView height is less than screen, the image display width will be less than screen too, which causes the background green color shows.
To ensure the imageView fits the screen, you can add border on it:
imageView.layer.borderColor = UIColor.red.cgColor
imageView.layer.borderWidth = 1.0
Try to use in your imageView{
imageView.contentMode = .scaleAspectFill
}

Round Border With Background Image Button swift

I'm using this code to create round Border Button With Background Image
{
Button.layer.cornerRadius = clear.frame.width/2
Button.clipsToBounds = true
Button.layer.borderWidth = 5.0
Button.layer.borderColor = UIColor(displayP3Red: 91/256, green: 87/256, blue: 115/256, alpha: 1.0).cgColor
}
And this is the outcome:
I want create a margin between the Button and the rounded border to this outcome:
Thanks in advance
Instead of setting background image you can set it as image to UIButton.
let image = UIImage(named: "imageName")
yourButton.setImage(image, for: .normal)
Using Storyboard, from attribute inspector.
You can also set content, title and image insets (top, left, bottom and right) from size inspector.
Set Content Insets to set image and title both at a time.
Set Title Insets to set title at a time.
Set Image Insets to set image at a time.

how to set a UIImageView tintColor from another image pattern color in Swift

I am looking to set the UIImageView tintColor from a UIColor pattern image. So far it works perfect with UILabels
let image = UIImage(named: "my_pattern_image")
let pattarnColor = UIColor(patternImage: image)
let newImage = UIImage(named: "some_template_image").withRenderingMode(.alwaysTemplate)
let imageView = UIImageView(image: newImage)
imageView.tintColor = pattarnColor
The above works well with UILabels, as well as testing the rendering mode, setting the color to UIColor.red and image changes the tintColor. It just doesn't work with pattern image color for some reason, and sets this to white.

tintcolored area to have border in uiimageview

let templateImage = UIImage(named:"marker")?.withRenderingMode(.alwaysTemplate)
self.imageView.image = templateImage
self.imageView.contentMode = .scaleAspectFit
self.imageView?.tintColor = UIColor.red
self.imageView?.tintAdjustmentMode = .normal
self.imageView.backgroundColor = .clear
I'm using Swift 3
But what I require is the tintcolored area border to be filled with black color as below screenshot:
Marker icon is here:
Are you trying to border the content of the image. If yes, then it is not possoble.
You can give border to imageview using below lines:
self.imageView.layer.borderwidth = 1 //as per your requirement
self.imageView.layer.borderColor = UIColor. blackColor().CGColor

rounded imageView 's border is not smooth after setting image property

class PPAvatarCollectionCell: UICollectionViewCell {
var imageView:UIImageView!
override init(frame: CGRect) {
super.init(frame: frame)
imageView = UIImageView(frame: CGRect(origin: CGPointMake(0, 0), size: CGSizeMake(frame.size.width, frame.size.height)))
self.addSubview(imageView)
imageView.contentMode = .ScaleAspectFill
imageView.backgroundColor = UIColor.whiteColor()
imageView.layer.borderColor = UIColor.greenColor().CGColor
imageView.layer.borderWidth = 10
imageView.image = UIImage(named: "demo")
imageView.layer.cornerRadius = frame.size.width*0.5
imageView.clipsToBounds = true
}
the border is smooth and great with above code
but after add imageView.image = UIImage(named: "demo")
the imageView's border is no longer smooth.
Why did this happen ?
UPDATE:
Seems something wrong with layer.radius ,the border is smooth even with image property set on imageView after remove imageView.layer.cornerRadius = frame.size.width*0.5
UPDATE 2:
turn out to be something wrong with UICollectionViewCell , imageView is a part of UICollectionViewCell
u can use 2 imageview 1 to show image and second to hide and show circular image.
1st imageview show image as it is.
2nd imageview which will have white background and green circle above 1st image and middle of image will be transparent
Basically, your image should be a square. Than do these:
imageView.layer.cornerRadius = self.frame.height / 2
imageView.layer.masksToBounds = false
imageView.clipsToBounds = true
imageView.image = "yourSquareImage"
Check the full solution with easy using extension:
https://stackoverflow.com/a/36102901/2125010

Resources