horizontal and vertical separator line in UICollectionview - ios

I want to make separator lines between collection View cell with a specific color(grey), without changing the BG color of Collection View.
enter image description here

You can use the UIView inside the collectionViewCell and width 1px or 2px

Change the background color of the superview to grey and add edge insects to the collectionView using the below code in viewDidLoad life cycle method:-
private let spacing:CGFloat = 2.0
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: spacing, left: spacing, bottom: spacing, right: spacing)
layout.minimumLineSpacing = spacing
layout.minimumInteritemSpacing = spacing
self.collectionView?.collectionViewLayout = layout

Related

UICollectionView content hidden by TabBar

The problem
Hello, community! I created a UICollectionView programmatically and want it to display images that take the full size of the screen, surpassing the top safe area but respecting the bottom safe area, since at the bottom I have TabBar. The issue with the current UI is that I got it to ignore the top safe area, but the tab bar hides part of the content since the collection takes the size of the whole screen (Video). I'm trying so that my UI looks like the TikTok home page where the collectionView ends when the tab bar starts.
My Code
In ViewDidLoad:
let layout = UICollectionViewFlowLayout()
self.edgesForExtendedLayout = UIRectEdge.bottom
layout.scrollDirection = .vertical
layout.itemSize = CGSize(width: view.frame.size.width, height: view.frame.size.height)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView?.register(PostCollectionViewCell.self, forCellWithReuseIdentifier: PostCollectionViewCell.identifier)
collectionView?.isPagingEnabled = true
collectionView?.dataSource = self
collectionView?.showsVerticalScrollIndicator = false
collectionView?.showsHorizontalScrollIndicator = false
view.addSubview(collectionView!)
In ViewDidLayoutSubviews:
collectionView?.contentInsetAdjustmentBehavior = .never
collectionView?.frame = view.bounds
What I've tried
layout.itemSize = CGSize(width: view.frame.size.width, height: view.frame.size.height - (self.tabBarController?.tabBar.frame.height)!)
Substracting height of tabBar to CollectionViewFlowLayout
collectionView?.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: self.tabBarController!.tabBar.frame.height, right: 0)
collectionView?.contentSize = CGSize(width: view.frame.size.width, height: view.frame.size.height - self.tabBarController!.tabBar.frame.height)
Many Thanks!
You should use auto-layout instead of setting the frame of your collection view manually. After view.addSubview(collectionView!) put:
collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
This pins the bottom anchor of the collection viewto the bottom safe area of your view, which should account for the tab bar.
You should remove collectionView?.frame = view.bounds from viewDidLayoutSubviews.
Also you should not set your itemSize in viewDidLoad because the view's frame may change. You can set it in viewDidLayoutSubviews.

Change the layout margins of a UITableViewController programmatically

The first image is how my cells are currently and the second image is how I want them to be. I have a UITableViewController and want to programmatically change the layout margins however it does not work
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor(named: "Gray")
tableView.backgroundColor = UIColor(named: "Gray")
tableView.contentInset.top = .padding
tableView.separatorStyle = .none
tableView.register(TaskCell.self, forCellReuseIdentifier: "taskCell")
tableView.layoutMargins = .init(top: 0, left: 20, bottom: 0, right: 20) // Does not work
}
Put a UIView inside the cell as background view and give margins to that view by adjusting its size in the storyboard. Then change the colour opacity of the cell's content view to 0

UITableViewController extra blank space at the bottom

I have a UITableViewController that shows about 20px height blank space at the bottom like so. Any Ideas how to remove it? I tried setting tableView.contentInsetAdjustmentBehavior = .never like suggested in other answers but no luck
Try
self.tableView.tableFooterView = nil or self.tableView.tableFooterView = UIView()
You can try setting bottom inset to 0
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

constrain a label inside of a UIView which is inside of a expandable collectionViewCell

I have an expandable/collapsible cell in Xcode, and I am trying to do what is said in the title. When I expand the cell, the text is centred inside of the cell and I'm not sure why because I have the label constrained inside of the UIView. I will leave code below,
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(SubView)
SubView.anchors(top: topAnchor, topPad: 0, bottom: nil, bottomPad: 0, left: leftAnchor, leftPad: 0, right: rightAnchor, rightPad: 0, height: 80, width: self.bounds.width - 20)
SubView.addSubview(textField)
textField.anchor(top: SubView.topAnchor, leading: leadingAnchor, bottom: bottomAnchor, trailing: trailingAnchor, padding: .init(top: 0, left: 0, bottom: 0, right: 0))
textField.centerYAnchor.constraint(equalTo: SubView.centerYAnchor).isActive = true
textField.centerXAnchor.constraint(equalTo: SubView.centerXAnchor).isActive = true
textField.translatesAutoresizingMaskIntoConstraints = false
}
this is how I create the anchors and addSubview.
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "profCell", for: indexPath) as! profCell
if indexPath.row == 0 {
cell.textField.text = "Skills & Preferences"
} else if indexPath.row == 1 {
cell.textField.text = "Bio"
} else if indexPath.row == 2 {
cell.textField.text = "Reviews"
}
return cell
}
and this is how I try to create the label text. I will leave the two images that show what I am asking.
this is how I want the cell to look even if it is expanded.
when it is expanded, the label moves to the center of the cell.
thanks for all of the help!
You need to understand a bit about Auto-Layout Constraints. If you are using Leading, Trailing, Top & Bottom constraints, you are not required to add Center X & Center Y anchors. But even if you add those extraneous constraints you need to be very careful so that they don't conflict with each other. For this purpose, you need to make some of them Low priority constraints while leaving some as High priority constraints or make them greater/equal or less/equal instead of explicit equal. This is a topic that I can't make completely understandable here in SO.
Secondly, you have pinned the leading, bottom & trailing constraints to the contentView of the UITableViewCell itself which is wrong according to your expectation. You either
change it to constraint the textField with respect to SubView's anchors here:
textField.anchor(top: SubView.topAnchor, leading: SubView.leadingAnchor,
bottom: SubView.bottomAnchor, trailing: SubView.trailingAnchor,
padding: .init(top: 0, left: 0, bottom: 0, right: 0))
and delete those textField.centerXAnchor & textField.centerYAnchor entirely.
Or
delete the line of setting the leading, trailing, top & bottom constraints entirely keeping only the center constraints.
Note: Use camelCased identifier for properties.

UICollectionViewCell starts after UICollectionViewReusableView width

I'm working on UICollectionView inside UITableViewCell. Using "UICollectionReusableView" as header view. The width of header view is 100. Before I introduce header view the CollectionViewCell started at the beginning of CollectionView, which is as expected. After I put header view, CollectionViewCell starts at end of header view width. I think somewhere position is not set properly.
CollectionView Scroll direction is Horizontal. I have used UICollectionViewFlowLayout to present the CollectionViewCell.
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 5.0, left: 5.0, bottom: 5.0, right: 5.0)
layout.minimumInteritemSpacing = 8.0
layout.minimumLineSpacing = 5.0
layout.scrollDirection = .horizontal
layout.sectionHeadersPinToVisibleBounds = true
collectionView.collectionViewLayout = layout
I have set the size of the header view using "referenceSizeForHeaderInSection" delegate.
Any hints where I might be wrong.
Instead of using UICollectionViewFlowLayout, you should subclass this and override the method
layoutAttributesForSupplementaryViewOfKind:
And give your frame accordingly

Resources