CollectonView "readjusts" origin after loaded into view - swift

I am making a custom sticker pack. It is an iMessage Extension app. I am subclassing UICollectionView rather than using the basic MSMessagesViewController. So this happens only when I select the chevron icon in the expanded view to change back to the compact view. What's going on(you can see in the gif below) is the stickers are place in the view and appear to be set in their location, but after a split second, they seem to readjust their positions...Is there something in CollectionView that I should be doing to prevent this?
I'm starting to think it could be a bug, because the header bar that contains the text field, iMessage app drawer icon, heart icon and camera icon seem to be cut-off about the same amount as the shift.
here is the code from github

The problem with your code is that the contentInset for your collectionView in your layout() call is 6 pixel off from the original position. That's why the animation adjusts the 6 pixel after the animation has finished.
Just change the UIEdgeInsets() in your layout() call inside the StickerCollectionVC to:
self.collectionView?.contentInset = UIEdgeInsets( top: screenW * 0.1 - 6,
left: screenW * 0.1,
bottom: 20 + (screenW * 0.1),
right: screenW * 0.1)

Related

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.

Half screen and full screen bottom sheet with material components for iOS (swift)

I'm using material components for iOS for making a draggable bottom sheet, that starts half screen (preview mode), and you can drag to full screen. But I don't know how, and the documentation is not very complete. https://material.io/develop/ios/components/bottom-sheet/
I present my DetailViewController within the MDCBottomSheetController:
let graphDetailViewController = UIStoryboard.detail().instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
let bottomSheet: MDCBottomSheetController = MDCBottomSheetController(contentViewController: graphDetailViewController)
bottomSheet.preferredContentSize = CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height / 2)
present(bottomSheet, animated: true, completion: nil)
With this code, my vc is appearing half screen, but it's not possible to drag it up to full screen.
Is anyone experiences with material components (bottom sheet) for iOS and could help me?
Thanks a lot!
Everything is right except you set the preferredContentSize to be 1/2 the screen height.
bottomSheet.preferredContentSize = CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height / 2)
Is the line causing your error. That line is telling the bottom sheet that there is no more content to scroll. You can delete it. If you want to have your contentViewController be a scroll view you may want to use the trackingScrollView property.

Toggle Height/.Top Constriants of NSTableView by pressing a button

so i have a NSTableView that presents information, then i have an add button that shows a view that is hidden behind the tables view. It is shown by changing the tableview's height to lower and setting its hidden property to false. I am running into the problem where i have constraints set in storyboard for the tableview and when i try change its frame programatically the storyboard constraints overrule the frame i set in code. What i need is for the constraints to be:
When the tableview is at full size:
Left: 200
Right: 0
Bottom: 20
Top: 20
When the tableview is shrunken to show the view below:
Left: 200
Right: 0
Bottom: 20
Top: 110
I want to use constraints because when people resize the window the table view should scale.
Basically i need to be able to toggle the height constraint (or the .Top constraint) the rest should stay the same.

tvOS: UIScrollView doesn't scroll

I am trying to reproduce natively the TVML template that provides a grid of clickable images that extends beyond the screen's bounds. I am using a scroll view for this attempt, but I am unable to select elements that are added to the scroll view, but outside its visible area.
The sketch code using buttons for simplicity is as follows:
let dim = 50
for i in 0..<10 {
for j in 0..<10 {
let frame = CGRect(x: i * (dim + 10), y: j * (dim + 10), width: dim, height: dim)
let button = UIButton(type: .System)
button.frame = frame
myScrollView.panGestureRecognizer.allowedTouchTypes = [UITouchType.Indirect.rawValue]
myScrollView.addSubview(button)
}
}
The scroll view is sized such that only half of these buttons are visible. Why is the scroll view not scrolling to the buttons outside this area (using Siri remote)?
I thought the panGesture touchType might help, but it didn't.
Am I missing something obvious?
Set contentSize property to your scrollview. Make sure all components comes under given content size.
myScrollView.contentSize = CGSizeMake(1880, 2000)
It would actually be way easier to just use a UICollectionView. If you add an image to each cell, you'll get exactly the behavior you want after adjusting the collection view to what you want.
This tutorial kind of explains how it works. http://www.brianjcoleman.com/tutorial-collection-views-using-flow-layout/