Constraint View Inside Content Of Image View Swift - swift

I have an image view that gets a dynamic image, and uses aspect fit to determine the frame of the image itself. I am trying to constraint a view inside of the image view, but when the image dynamically resizes, the view inside of it doesn't also dynamically resize when my constraints are just set to the borders of the image view.
Is there any way to set the constraint to the content size that the image is after it resizes, inside interface builder?
Thank you!

When you set the image view’s frame, pick an initial height or width—not both. Then, get image.size and calculate the proportional other dimension. For example:
let width = 100
let height = width * image.size.width / image.size.height *
imageView.frame.size = CGSize(width: width, height: height)
imageView.contentMode = .scaleAspectFit
imageView.image = image
Now your imageView is exactly the size of your image. You can do the same thing with a fixed height by simply replacing “height” with “width” and vice versa in the code above.
Hope this is what you’re looking for.

Related

How to Set UIImageView in circle Swift 5

This code is only working with fix height and width. How can Round UIImageView when we use multiplier?
Here, is the code that Circle my ImageView. Width and Height is 300.
class MusicViewController: UIViewController {
#IBOutlet weak var imgAlbum: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imgAlbum.layer.borderWidth = 1
imgAlbum.layer.masksToBounds = false
imgAlbum.layer.borderColor = UIColor.black.cgColor
imgAlbum.layer.cornerRadius = imgAlbum.frame.height/2
imgAlbum.clipsToBounds = true
}
}
If I change my height and width to multiplier like. Now, the multiplier of Height 0.5 and width 0.8.
(Proportional height and width to SuperView)
As #RajaKishan and #aiwiguna both said that you can't get a square image with both proportional height and width constraint together because then you can not get a round circle with different height and width.
You can set width or height proportional to superview and set the aspect ratio to 1:1 then you can change the multiplier and get the circle properly. You can check to attached image for constraints
then set cornerRadius in viewDidLayoutSubviews(), not in viewDidLoad(). (viewDidLoad() is called before the layout constraints change the view height, and only once. viewDidLayoutSubviews() is called any time your view's geometry changes, so you should invoke any layout logic there.
override func viewDidLayoutSubviews() {
self.yourImageView.layer.cornerRadius = self.yourImageView.bounds.height/2
self.yourImageView.clipsToBounds = true
}
you will not get a square by using proportional width and proportional height constraint together like that, in different device it will have different height and width
my suggestion is to use only one proportional width or proportional height (which one you want) and use aspect ratio constraint 1:1 on the view

Center a subview horizontally with custom vertical distance | Swift

I have the following subview that I would like to centered in horizontally on the screen and set the vertical distance manually.
let customImageView = AnimationView(name: "image")
customImageView.frame = CGRect(x: -140, y: 40, width: 700, height: 700)
customImageView.contentMode = .scaleAspectFill
self.view.addSubview(customImageView)
Is it possible to ensure that the vertical distance set will account if the screen size of the phone is smaller.
This sounds like a job for Auto Layout (https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/index.html), which is Apple's constraint-based system made specifically to dynamically size views in relation to other views and screen sizes. In your case, you could set a greater than/less than to constraint to the top of the view and your subview, so that it will be at least a certain amount of spacing with the option to grow if the screen is larger/smaller.
If you would like to still do frame based layouts (e.g. creating a CGRect/frame), you'll have to do the math yourself based on the view.frame.size.height value. Be aware that a view controller's view frame might not be set before viewDidLayoutSubviews is called.

Rotating a UICollectionView leads to error

When I rotate the device with a UICollectionView I get an error:
the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
I think that this mistake is within my UICollectionViewDelegateFlowLayout where I try to set the height
if UIApplication.shared.statusBarOrientation.isLandscape {
height = min( Constants.screenWidth, Constants.screenHeight )
}
return CGSize(width: view.bounds.width, height: (height - 300) / 1 )
as you can see I've tried to alter the height for the orientation, but ultimately this does not change as I thought it should.
Complete code is in this GitHub link: https://github.com/stevencurtis/CollectionViewRotate
I want to stop getting this error, and ultimately because the height is incorrectly set when I rotate the device the image is larger than the bounds of the view.
How can I set up my flow layout so this doesn't happen.
The height of the screen is irrelevant. What you really care about is the height of the collectionView.
Use:
let height = collectionView.bounds.height
By using that value, the height will be correct when you rotate the phone without your having to check for it explicitly. You'll still want to adjust for insets if you have any.
You should also use collectionView.bounds.width for the width in place of view.bounds.width.

Adjust Auto-Layout inside UITableViewCell

So I am creating a forum. When a post includes an image I would like to keep the aspect ratio of the downloaded image while making the image's width constant at 75% of the cell's width.
I created an outlet for the imageHeightConstraint in my custom cell class and tried the code below in the cellForRowAtIndexPath method.
let newHeight = (image!.size.height/image!.size.width)*cell.uploadedImageView.frame.width
cell.imageHeightConstraint.constant = newHeight//Calculate the new height
The code above doesn't adjust the image size at all.
Currently I have the constraint for image width to be 75% of cell's width and height equal 200.
I also already have all the code to adjust the tableviewcell automatically depending on content and that is working fine.
Thanks for any help.
Try
if let image = image {
cell.image.heightAnchor.constraint(equalTo:cell.image.widthAnchor, multiplier: image.size.height/image.size.width).isActive = true
}
If you want to also limit the height to 200, then you can also add
cell.image.heightAnchor.constraint(lessThanOrEqualToConstant:200).isActive=true

Title Image UINavigationBar

I'm trying to add a title image to my nav bar in the entirety of my app. The below line of code in my App Delegate works, but the image is tiled to fit... Is there a way I can set the dimensions and position?
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "tmp logo.png"), forBarMetrics: .Default)
You can set the size of image, by setting the size of UIImageView.
var imageNew = UIImageView(frame: CGRectMake(10, 10, 100, 100));// define
the size of the view here.
var image = UIImage(named: "myImage.png");
imageNew.image = image;
self.view.addSubview(imageNew);
Pl. refer to the below stackoverflow response, which provides the solution
How do set a width and height of an Image in Swift