How can I push my collectionView in the background programmatically? Right now it covers my bottom-bar. It's easy in Storyboard but I have no idea how do get it done programmatically.
let theCollectionView: UICollectionView = {
let v = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout())
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .white
v.contentInsetAdjustmentBehavior = .always
v.layer.cornerRadius = 30
return v
}()
Use
self.view.sendSubviewToBack(_ with: UIView)
method if I got right your problem and there's another view in back of the collection view.
So for example your view has a view of the DashboardController etc. So in block that you wish you must
self.view.sendSubviewToBack(theCollectionView)
I hope it helps.
There's some to-do to achieve this.
You can hide your collectionView
yourCollectionView.isHidden = true
You can remove your UICollectionView from superview
yourCollectionView.removeFromSuperView()
Or you can make your UICollectionView as Optional.
Then you can removeFromSuperView and set nil.
yourCollectionView.removeFromSuperView()
yourCollectionView = nil
Then you can set nil and removeFromSuperView
When you use again, so must init again.
Related
I am new in IoS programming. I am involved in an existing project, and I am facing a problem.
UIRefreshControl is blocking views below. Like image attached (UIRefreshControl is transparent black).
UIRefreshControl is blocking views below it. But when I am doing some little scroll, the UIRefreshControl is disappeared, and then the button below is clickable.
Here is some snippet of the code.
CommentView.swift
lazy var collectionView: UICollectionView = {
layout.scrollDirection = .vertical
let collectionView: UICollectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.isUserInteractionEnabled = true
collectionView.backgroundColor = .white
collectionView.refreshControl = refreshControl
collectionView.registerCustomCell(CommentCell.self)
collectionView.register(CommentHeaderView.self, forSupplementaryViewOfKind: CommentView.className, withReuseIdentifier: CommentHeaderView.className)
return collectionView
}()
lazy var refreshControl: UIRefreshControl = {
let refresh = UIRefreshControl()
refresh.tintColor = .primary
refresh.addTarget(self, action: #selector(handleRefresh), for: .valueChanged)
refresh.backgroundColor = UIColor.blackTransparent
return refresh
}()
The blocked views are declared inside CommentHeaderView.swift.
EDIT
I have tried
refresh.layer.zPosition = -100
but still not working.
Anyone can help me? Thanks in advance!
I tried solution below, and it works perfectly.
refreshControl.layer.zPosition = -1;
refreshControl.userInteractionEnabled = NO;
UIRefreshController goes over the UICollectionView
Thanks all!
I am trying to build a view controller with collection view that comes with a custom sticky segment control header. Tapping on a segment in the segmented control, will refreshes the data in the collection view and also resets the content offset to zero.
While doing this i ran into an issue with navigation bar transition from large to small and vice versa.. As you can see here, the transition is jumpy and not smooth at all..
I was able to resolve this issue by changing the below line:
edgesForExtendedLayout = []
to this:
edgesForExtendedLayout = .all
But, adding the above line is messing up my attempt to reset the content offset to zero when i tap on a segment in the segment control. As you can see below:
If its not clear in the above gif, when i tap on the segment, the collection view scrolls along with the navigation bar.. and the content offset is still a bit wrong.. I want to keep the header where it is before the segment was tapped on and only reset the collection view content offset.
I also tried to set extendedLayoutIncludesOpaqueBars to true and false but had no effect. I've also tried to play with contentInsetAdjustmentBehavior of collection view but not much luck with that as well.
Here's the code where I am setting up the view.
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationItem.title = "Sticky Section Headers"
self.edgesForExtendedLayout = .all
self.extendedLayoutIncludesOpaqueBars = true
self.navigationItem.searchController = UISearchController()
setUpNavBarAppearance()
setUpCollectionView()
configureDataSource()
}
func setUpCollectionView() {
let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout())
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
collectionView.backgroundColor = .systemBackground
collectionView.register(ListCell.self, forCellWithReuseIdentifier: ListCell.reuseIdentifier)
collectionView.register(SegmentedControlSupplementaryView.self,
forSupplementaryViewOfKind: sectionHeaderElementKind,
withReuseIdentifier: SegmentedControlSupplementaryView.reuseIdentifier)
self.view.addSubview(collectionView)
collectionView.refreshControl = refreshControl
self.collectionView = collectionView
}
This is what I am doing when I tap on a segment in the segment control.
#objc func segmentChanged() {
self.collectionView?.setContentOffset(.zero, animated: true)
}
I've also tried to use scrollTo methods on collection view but the result has been the same.. I've also tried to set collection view bounds to safe layout guides top and bottom anchors.. that didn't help much either..
Here is the gist file with full code for the view in case if you would like to try it.
https://gist.github.com/prasadpamidi/829e636d4697fda025bb0795ee81e355
There is not much documentation or guidelines around how to do this properly.. I was able to reproduce this with the Apple's sample app itself.
Appreciate any help with resolving this issue..
Edit1: I am not using a storyboard everything has been added programmatically
Edit2: Updated code snippet
Hello, i've got the following issues with UICollectionView
The CollectionView won't allow you to scroll no matter what you do.
Sometimes the cells even disappear complete leaving you with a white background CollectionView.
The problem occurs when you try to scroll up to view the cells that are not visible.
I've create a CollectionView which has different type of cells.
The CollectionView is nested inside a ViewController below an ImageView.
Constraints are added and they work just fine.
How do i make it scrollable?
GIF representing the problem
ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.navigationItem.title = "Featured"
self.view.backgroundColor = UIColor.white
// Add UImage carousel
let carousel: UIImageView = {
let imageView = UIImageView()
let image = UIImage()
imageView.backgroundColor = UIColor.purple
return imageView
}()
self.view.addSubview(carousel)
carousel.translatesAutoresizingMaskIntoConstraints = false
// Add CollectionView
let featuredControllerLayout = UICollectionViewFlowLayout()
// Add CollectionViewController
featuredControllerLayout.scrollDirection = .vertical
let featuredController = FeaturedCollectionViewController(collectionViewLayout: featuredControllerLayout)
guard let featuredView = featuredController.collectionView else { return }
self.view.addSubview(featuredView)
featuredView.translatesAutoresizingMaskIntoConstraints = false
// Setup Constraints
if #available(iOS 11.0, *) {
let guide = self.view.safeAreaLayoutGuide
let guideSize = guide.layoutFrame.size
carousel.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
carousel.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
carousel.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
carousel.frame.size.height = guideSize.width/2
carousel.heightAnchor.constraint(equalToConstant: carousel.frame.size.height).isActive = true
featuredView.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
featuredView.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
featuredView.topAnchor.constraint(equalTo: carousel.bottomAnchor).isActive = true
featuredView.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
}
}
Heyy what you are telling is you are not able to scroll up and see the entire collection view items ?
if that is the case then you have not accounted the height of tabbar controller so you can enable translucent for example I have a git repo I made you can check and let me know if you are stuck anywhere
https://github.com/dwivediashish00/socialApp
I'm extending the UIView to create a custom view that will be used inside my all UIViewControllers in the app, this is created like:
extension UIView {
class func customBar() -> UIView {
let barview = UIView()
barView.backgroundColor = .red
barView.translatesAutoresizingMaskIntoConstraints = false
}
}
The caller of this method is always a UIViewController, I want to reference the caller ViewController's view's dimensions to add a constraints for the bar, this way it will always show at the same location. So I tried this:
class func customBar() -> UIView {
let barview = UIView()
barView.backgroundColor = .red
barView.translatesAutoresizingMaskIntoConstraints = false
barView.bottomAnchor.constraint(equalTo: barView.superView!.bottomAnchor, constant: -8).isActive = true
barView.rightAnchor.constraint(equalTo: barView.superView!.rightAnchor).isActive = true
barView.leftAnchor.constraint(equalTo: barView.superView!.leftAnchor).isActive = true
barView.heightAnchor.constraint(equalToConstant: 44).isActive = true
}
But this will cause a crash because when I call this function, it's not yet added to the viewController's view, thus, the superView is nil. Here's how I call it inside of viewDidLoad()
let customBar = UIView.customBar()
self.view.addSubview(customBar)
Please, if you have any question, ask me instead of downvoting.
But this will cause a crash because when I call this function, it's not yet added to the viewController's view, thus, the superView is nil.
Since the constraint-adding code must not run until the view has a superview, put it in the view's didMoveToSuperview.
Is it possible to display an UIView on top of a container View?
I want to add a view with a few opacity background to still see my container View. But everything i tried made either my containerView disappear completely or on top of my View. I tried via Storyboard and code.
I'm sure I'm missing something.
just add your view to the view property of your container controller's container
simple:
let viewYouWantToAddSubviewTo = parent?.view
detail:
import UIKit
class CustomNavigationViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
func setupViews() {
let layout = UICollectionViewFlowLayout()
let rootVC = HomeCollectionViewController(collectionViewLayout: layout)
viewControllers = [rootVC]
let v = UIView()
v.backgroundColor = UIColor.blue
v.layer.opacity = 0.4
v.translatesAutoresizingMaskIntoConstraints = false
// add your view to this view of the controller's container
let vv = (parent?.view)!
vv.addSubview(v)
// constraints for v
v.leftAnchor.constraint(equalTo: vv.leftAnchor).isActive = true
v.rightAnchor.constraint(equalTo: vv.rightAnchor).isActive = true
v.topAnchor.constraint(equalTo: vv.topAnchor).isActive = true
v.bottomAnchor.constraint(equalTo: vv.bottomAnchor).isActive = true
}
}
result: