ios Custom UIButton scale setImage down not working - ios

I have problem in scale down setImage size if I add UIButton by programmatically whereas if I add the UIButton by storyboard, the UIButton size remain correctly.
The Image is 128x128, I want to shrink down to 30x30
Top one is created from using storyboard, Bottom is created from using programmatically
These are the methods I tried, none able to shrink the image...
let checkBox = UIButton(type .system)
checkBox.sizeThatFits(CGSize(width: 30, height: 30))
checkBox.contentMode = .scaleAspectFit
checkBox.contentVerticalAlignment = .fill
checkBox.contentHorizontalAlignment = .fill
checkBox.heightAnchor.constraint(equalToConstant: 15.0)
checkBox.widthAnchor.constraint(equalToConstant: 15.0)
Image is set/update on runtime
checkBox.setImage(UImage, for: .normal)
With suggest using checkBox.frame.size.width, checkBox.frame.size.height
the image becomes correct size but the UIButton now have extra space.
Like the image below. I update image on touchUP UIButton

What about this:
checkBox.frame.size.width = 30
checkBox.frame.size.height = 30

The Image is 128x128, I want to shrink down to 30x30
One obvious solution, in that case, is to shrink the image. Redraw the image into a 30x30 graphics context at 30x30 size and extract the resulting image. Now you have a 30x30 image. That's the best way; it is a waste of memory to hand the runtime a way oversized image and ask it to display it smaller.
Now, if you want to use constraints to size and position the button, that's fine, but you are not doing that correctly. First, you have forgotten to set the button's translatesAutoresizingMaskIntoConstraints to false. Second, you have to supply enough constraints not only to set the button's height and width but also its horizontal and vertical position.

Related

how to remove white space in circle UIImageView?

I wanted to circle my UIImageView and added this code :
profileImage.layer.cornerRadius = profileImage.frame.size.height/2
profileImage.clipsToBounds = true
and it work perfectly, but when images are horizontal, I get this picture:
as you can see, there is white space at the bottom and top of my circle image view. but what I really wanted was a circle filled with my image!
I've tried changing "content Mode" from attribute inspector, but I didn't get any answer! how can I fix this issue?
You have already set the clipsToBound property. So just update the contentMode.
I you don't want to distort image's scale:
profileImage.contentMode = .scaleAspectFill
If image's scale does not matter, you also can use:
profileImage.contentMode = .scaleToFill
Follow the link for more details: https://useyourloaf.com/blog/stretching-redrawing-and-positioning-with-contentmode/
Use below code:-
profileImage.clipsToBounds = true
profileImage.contentMode = .scaleAspectFill
Set the contentMode of your UIImageView to scaleAspectFill
profileImage.contentMode = .scaleAspectFill
Keep in mind that using this contentMode option some portion of the content may be clipped to fill the view’s bounds.
You can set the Different Content Mode as per you choice from StoryBoard:
You can select
Scale to Fill
Aspect Fill
or any other content mode by using trial and Error method which suits you
Hope it Helps.
If I choose Aspect Fit, I'm getting blank space in top and bottom of the imageview.
Can I trim that space anyhow?
Is there any programatically constraints I can set to re-scale imageview according to actual image size?

How to add many UIImageViews inside ScrollView?

I will have about 12+ (any multiple of 4 per row) images that I want to have inside a ScrollView. See example below, the 12 circles (with the last row cut off a bit) are the UIImageViews.
I am not able to add the UIImageViews inside the ScrollView. The width,height of the scrollView is 250,250
This is what I have tried:
func setScrollView() {
scrollView_avatars.contentSize = CGSizeMake(250,300);
let imgView1: UIImageView = UIImageView(image: UIImage(named: "img1.jpg"))
let imgView2: UIImageView = UIImageView(image: UIImage(named: "img2.jpg"))
let imgView3: UIImageView = UIImageView(image: UIImage(named: "img3.jpg"))
scrollView_avatars.addSubview(imgView1)
scrollView_avatars.addSubview(imgView2)
scrollView_avatars.addSubview(imgView3)
}
iOS noob here
My code above gives me very bizarre output, i think the sizes of the UIImageViews are not set correctly.
I dont also know how to position the UIImageViews within the ScrollView like only part of a image is visible.
As #rmaddy says, you need to set the frame properties on your views. You will need to do math to calculate their positions based on row/column position in your grid of views.
If you're using AutoLayout you may need to generate constraints to set the view's sizes and positions. (I haven't tried to add views to a scroll view in code with AutoLayout before, so I'm not totally positive what happens if you just set their frames without defining constraints.)
You need to declare your image views like this:
if let image = UIImage(named: "myImage"){
let imageView = UIImageView(frame: CGRectMake(yourXpoint, yourYpoint, image.size.width, image.size.height))
imageView.image = image
}
where yourXpoint and yourYpoint is where you want the image origin to be set, this way you are saying to swift where do you want the images to be place and what is its size.
The if let is just to be safe in case the image does not exist.
Another good option is to set constrains as it will make your app have a nice layout in al different screen sizes and orientation

iOS Xcode fill background image based on percentage

Looking to fill the background image in Xcode iOS based on a percentage.
The Constraints
The image must fill the entire screen (centered), so that the smallest dimension (width or height) fits. (Even if image becomes pixelated because it has a small file size.)
This must work universal, both iPad and iPhone devices.
Examples
You can use UIImageView. To make an image fill the screen, set imageView.contentMode to ScaleAspectFill.
To make UIImageView fill the screen, use auto layout. The easiest way is to add 4 constraints for UIImageView. Spacing to nearest neighbor = 0 will do. Like so:
Or leading/trailing/top/bottom space to superview = 0, if you like.
Set height and width of your image view to the width and height of your screen
var image:UIImageView = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
You want to make a UIImageView with the contentMode set to .ScaleAspectFill and add it as the bottom subview of your view.

Why UIImageView's image can extend beyond itself?

Previously, i thought UIImageView's image cannot extend beyond the view. However, recently i read the book Programming in IOS8, it says:
You should also pay attention to a UIImageView’s clipsToBounds property; if it is false, its image, even if it is larger than the image view and even if it is not scaled down by the contentMode, may be displayed in its entirety, extending beyond the image view itself.
And then i did some simple test, that's true.
Here is the question: What's the purpose for Apple to design UIImageView with this feature? I, for now, cannot find the advantage of it. I think this makes contentMode a fake property.
Here is an example:
It is a refreshView with customized pull-to-refresh animation above in a UITableView. I set up a UIImageView with the same size as the refreshView and add it as a background view. Relevant code here:
let imgView = UIImageView()
imgView.frame = self.bounds
imgView.image = UIImage(named: "refresh-view-bg.png")
imgView.contentMode = .ScaleAspectFill
imgView.clipsToBounds = true
self.addSubview(imgView)
It works fine:
However, when i annotate this line:
imgView.clipsToBounds = true
It turns like that:
The image extend beyond the UIImageView
ps: I'm not using autoLayout in this demo
This is due to autolayout. UIImageView returns its -intrinsicContentSize by the calculation of image size. If you want to avoid that you should set a width (or height) constraints, or play with content hugging/compression resistance priorities if you want to share or not the space when you view is laid out with other views around.

Change the size of the background image codewise from a label in swift

In the app that I am making I have some background images for some labels.
The image is 500x550 pixels but my label is 250x275 pixels.
How can I make the code change the width and height of the background so it fits the size of the label?
Code I have now:
lblMainNumber.backgroundColor = UIColor(patternImage: UIImage(named: "image.png")!)
It is easier to have a container UIView with size of 250 x 275 that will contain 2 subviews with same size:
1) a UIImageView with your image (set its contentMode to UIViewContentModeScaleAspectFit)
2) UILabel with your text. It will be on top of the UIImageView to stay visible.
You would possibly want to set both subviews (via autolayout) to match the size of container view.
Pattern image approach is pain in the ass and not wort the effort.

Resources