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

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.

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.

UITableView ContentInset and ContentOffset

I'm having trouble with the contentInset property. I have a UITableView, with dynamic cell sizes (AutoLayout). I'm setting the contentInset property to leave some space above the top of the content. But I'm getting the following result:
The content is in blue, the content inset in purple. When the table view first appears, it is in the left situation. I can scroll to get to the right situation, that is working, but I would like the table view to appears directly as in the right illustration — which I thought would be the default behavior.
How can I achieve that?
Not sure if it's the best way but I fixed it by adding:
tableView.contentOffset.y = -70
after the line:
tableView.contentInset = UIEdgeInsets(top: 70, left: 0, bottom: 50, right: 0)
you can scroll programmatically when the view loads.
tableView.setContentOffset(CGPoint(x: 0, y: -70), animated: false)
This is how it can be fixed easily from the Storyboard:
Table View > Size Inspector > Content Insets: Never
You provide an UIEdgeInset object,
UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0)
The top property shows the distance from top of content to top border of the area.

CollectonView "readjusts" origin after loaded into view

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)

Header View behind NavigationBar [duplicate]

I have a UICollectionView that is the entire view but it lives inside "view" (it is not UICollectionViewController). Adding a cell to this collection view shows it in the following order in storyboard:
This is how it looks in the emulator:
I don't understand how to get rid of that view. All the insets are at zero in Storyboard Size Inspector for collection view. Just to be sure, I also have the following code:
override func viewWillLayoutSubviews() {
let layout = self.collectionViewProducts.collectionViewLayout as! UICollectionViewFlowLayout
let containerWidth = UIScreen.main.bounds.size.width - 40.0
let itemWidth = (containerWidth / 3.0)
let itemHeight = (itemWidth / 0.75) + 30
layout.sectionInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
layout.itemSize = CGSize(width: itemWidth, height: itemHeight)
}
How can I get rid of that top padding?
You can fix top padding issue by considering one of the following method.
Method 1: Natural way to fix your problem by setting up your collectionView dimensions properly from StoryBoard.
Method 2: **Updated**
You can validate collection frame in viewDidLayoutSubviews or viewWillLayoutSubviews
override func viewDidLayoutSubviews() {
collectionView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
}
Method 3: You can Adjust Scroll View Insets from your StoryBoard Attributes Inspector.
Method 4: You can fix this issue programatically by adjusting CollectionView contentInset.
collectionView.contentInset = UIEdgeInsets(top: **Any Value**, left: 0, bottom: 0, right: 0)
Output with top padding 5:
Output with top padding 44:
Output with top padding 64:
I think because this viewController is embedded in a navigationController. Let select this viewController in the storyboard and uncheck Adjust Scroll View Insets:
There is one more way to resolve this issue and that is selecting collectionView -> scrollView Content Insets -> "Automatic" to "Never".
By default scrollView Content Insets value is Automatic. Please check the below image.
For more details check: UIScrollView.ContentInsetAdjustmentBehavior
https://developer.apple.com/documentation/uikit/uiscrollview/2902261-contentinsetadjustmentbehavior
Make your Navigation Controller > NavigationBar translucent by unchecking the Translucent check box in IB > Attribute Inspector, and it will work.
I also had the same problem, and i fixed it with a way totally ridiculous solution.
My collectionView contained several sections which had no title & no item cells.
The top, bottom inset values of the section insets were 10 respectively.
so each empty section charged height of 20 pixels.
I had 4 empty sections, and therefore, 80 top margins in the collection view.
Hope you check this as well if none of the above solutions works.

Swift - Using autoLayout on custom Cell

I'm trying to center horitzonally a button into my custom cell but I can't.
The constraits are OK but doesn't work...
Some screenshots:
What i'm doing wrong?
Thanks!
EDIT1
I'm cheking the TableView, View and the Cell with:
println("TableView width: \(self.tableView.frame.width)")
println("View width \(self.view.frame.width)")
println("Cell width: \(cell.frame.width)")
TableView width: 600.0
View width 375.0
Cell width: 600.0
Cell width: 600.0
TableView and Cell are longer than the view. I think that this is the problem. Why isn't my cell fitting to the width screen?
Maybe the tableView is wider than the window. Try adding top, bottom, left and right constraints to the tableView.
If this is not the case, please give some more information about the constraints you added and the way you added the number label at the left of the cells.