Rotating a UICollectionView leads to error - swift

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.

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

NSGridView custom view intrinsic size

I'm building a simple NSGridView, and want to have a custom NSView as each element of the grid. Eventually, each NSView will be a xib based label (NSTextField) centered in the NSView.
The problem I am having is with the intrinsic size of the NSView. I want to define the size of the NSView and have auto layout work based on that. I added this code to the custom view (labelView):
override var intrinsicContentSize: NSSize {
return NSSize(width:100, height:100);
};
And it is indeed called; but apparently ignored. As a test, I have on the same row some other labels, and the height for the row is always set to the largest of the row text heights (including the label in the custom view); but the length is set to the longest of column text fields, ignoring the label in the custom view. And anyway, I want to arbitrarily make the NSView a certain height and length, as I tried (but failed) to do with the intrinsicContentSize.
NSStackview seems to do the right thing; but NSGridView does not.
I can force the width of a particular column with
grid.column(at:0).width = 400;
but want I really want to do is define the size of the NSView, and let autolayout use that as a building block.
This strikes me as a conceptual error on my part, so if someone could explain these NSGridView-autolayout-NSView subtleties, I think many might benefit.
I was having the exact same issue, tried to use custom NSView's inside a NSGridView and couldn't get them to draw correctly. What finally worked for me was setting the following:
let gridSize = 5
let cellSize: CGFloat = 50
gridView.xPlacement = .fill // this was key part of the solution
gridView.yPlacement = .fill
for i in 0 ..< gridSize {
gridView.row(at: i).height = cellSize
gridView.column(at: i).width = cellSize
}
Note that I'm setting the size of each cell with the row height and column width of the NSGridView, and not using the NSView size, but this is the only way I got it working.

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.

Constraint View Inside Content Of Image View 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.

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