Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Padding around UIImageView. Swift 3

I have a UIImageView, where I don't want the image to 'touch' the edge of the view, rather have some 'padding' around it. However, I have tried the following, and for some reason it doesnt change:

@IBOutlet weak var pictureOutletOne: UIImageView!

//set the image
pictureOutletOne.image = UIImage(named: itemOne)

//set the padding
pictureOutletOne.layoutMargins = UIEdgeInsetsMake(10, 10, 10, 10);

I have also tried:

pictureOutletOne.translatesAutoresizingMaskIntoConstraints = false
pictureOutletOne.layoutMargins = UIEdgeInsets(top: 10, left: 100, bottom: 10, right: 0)

I have read alot about this, but these are the solutions I have found, but they aren't working. Using Swift 3.

Thanks so much.

like image 302
ios234975 Avatar asked May 28 '17 19:05

ios234975


People also ask

How to put a photo in UIImageView in Xcode 8?

Xcode 8 come with a series of easily accessible preview modes in interface builder. We will put a photo into an UIImageView when we first use the image, and doing so on an iPad is easier than an iPhone, though it will work on both. Right-click and save the image below: Click on Assets.Xcassets and drag the pizza file into the assets folder.

How do you round off the corners of a UIImageView?

Make UIImageView Corners Rounded I will now use the created in the above Swift code snippet UIImageView and will make its corners rounded by changing the value of CALayer cornerRadius and setting the UIImageView clipsToBounds value to true. myImageView.layer.cornerRadius = 100 myImageView.clipsToBounds = true

How to get a photo from the camera in Swift?

There are two ways to use the camera. The more powerful and advanced method is using AVFoundation, but it is also a very complicated way to get a photo out of the camera. The simpler way is the UIImagePicker. This is a quick way as Swift has simplified much of the code for it. It is also the best way to fetch photos from the photo album.

What is @ibinspectable attribute in UIView?

This inspectable attribute can be applied to other properties of UIView that are missing from Interface Builder. Let’s add a borderColor property. Since the @IBInspectable attribute builds upon the mechanism for user-defined runtime attributes, we are still limited to the types that can be set by Xcode’s Interface Builder.


2 Answers

Swift 4.2 & 5

let imageView = UIImageView()
imageView.image = UIImage(named: 
"image")?.withAlignmentRectInsets(UIEdgeInsets(top: -5, left: -5, bottom: -5, 
right: -5))

Insets should be given in negative value

like image 161
Barath Avatar answered Oct 02 '22 00:10

Barath


You can insert the imageView into a view and set constraints to sides, guaranteed approach :)

like image 39
Dominik Bucher Avatar answered Oct 02 '22 02:10

Dominik Bucher