Can't get rid of a UIviewcontroller header - swift

I am trying to get rid of a header(extra-padding) that appears on the top of my view when I run the simulator. I have tried by setting the layout reference size to zero but is not working. The collectionview is only a small part of my screen that is why I'm not sure what it is. The header looks like that of a navigationview.
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.headerReferenceSize = CGSize.zero
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
cv.dataSource = self
cv.delegate = self
cv.isPagingEnabled = true
cv.showsHorizontalScrollIndicator = false
return cv
}()

With translucent navigation view controller's view content starts from top of navigation bar. If you want to layout view to start from bottom of navigation bar, set isTranslucent to false (available in interface builder).
Use proper contentInset of collection view

thanks for the help! I finally realized what it was, I'm doing my views programmatically and didn't realize that my view was a navigationController.
Here's what did it for me:
self.navigationController?.isNavigationBarHidden = true;

Related

White Space at the bottom of a tableview

I have a viewcontroller with a tableview inside it. At the bottom of the tableview there is some white space that I want to get rid of and be replaced by my background colour.
I have added and customised the tableview programatically so I am looking for a programatic answer, as I did not use storyboard much for this. (I had only used it to setup my TabBarController and link it to navigation and view-controllers.)
Below is the some of the code I used to configure the tableview.
private let tableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.register(ProfileTableViewCell.self, forCellReuseIdentifier: ProfileTableViewCell.identifier)
tableView.backgroundColor = Constants.backgroundColor
return tableView
}()
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.frame = view.bounds
}
Im sure the answer is probably just a single line of code, but I couldn't find one that worked. Thanks in advance!
***EDIT
I have found a solution, but I still think there is a better way to solve this problem. The code below sets a UIView as the background of the tableview, then changes the colour of the UIView.
private let tableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.register(ProfileTableViewCell.self, forCellReuseIdentifier: ProfileTableViewCell.identifier)
let backgroundView = UIView(frame: CGRect(x: 0,
y: 0,
width: tableView.bounds.size.width,
height: tableView.bounds.size.height))
backgroundView.backgroundColor = Constants.backgroundColor
tableView.backgroundView = backgroundView
return tableView
}()
The default color of the containing view controller is showing, since the table is short. Try setting the view controller's view's background color in viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad();
view.backgroundColor = Constants.backgroundColor
}

Helping with swift layout (Simple for you not me lol)

Hi I am learning how to make newsfeeds and although things are going well I would like some help in terms of the layout of the feed itself. The feed clips to the safe areas of the simulator but I want to move the top section down about half way down the screen. I have made the table view using a XIB and created the table view programatically. How would one be able to shift the top section down? Here is the code:
HomeViewScene = UITableView(frame: view.bounds, style: .plain)
HomeViewScene.backgroundColor = Colors.white
view.addsubview(tableview)
let cellNib = UINib(nibName: "PosterCell", bundle: nil)
HomeViewScene.register(cellNib, forCellReuseIdentifier: "postCell")
var layoutguide:UILayoutGuide
layoutguide = view.safeAreaLayoutGuide
HomeViewScene.leadingAnchor.constraint(equalTo: layoutguide.leadingAnchor).isActive = true
HomeViewScene.topAnchor.constraint(equalTo: layoutguide.topAnchor).isActive = true
HomeViewScene.trailingAnchor.constraint(equalTo: layoutguide.trailingAnchor).isActive = true
HomeViewScene.bottomAnchor.constraint(equalTo: layoutguide.bottomAnchor).isActive = true
It looks to me like you're pretty close. The 2 main things I would suggest are:
1) You need to set translatesAutoresizingMaskIntoConstraints to false if you want your constraints to actually take effect.
2) If you want your table view under your other view, you need to set your table view's top anchor to be equal to your other view's bottom anchor.
Specifically, this is how I would change the code you posted to solve your problem:
HomeViewScene = UITableView(frame: view.bounds, style: .plain)
HomeViewScene.backgroundColor = Colors.white
view.addsubview(tableview)
let cellNib = UINib(nibName: "PosterCell", bundle: nil)
HomeViewScene.register(cellNib, forCellReuseIdentifier: "postCell")
var layoutguide:UILayoutGuide
layoutguide = view.safeAreaLayoutGuide
HomeViewScene.translatesAutoresizingMaskIntoConstraints = false //Add this line
HomeViewScene.leadingAnchor.constraint(equalTo: layoutguide.leadingAnchor).isActive = true
HomeViewScene.topAnchor.constraint(equalTo: otherView.topAnchor).isActive = true //You need to make sure the TOP of the table view is positioned right at the BOTTOM of the other view
HomeViewScene.trailingAnchor.constraint(equalTo: layoutguide.trailingAnchor).isActive = true
HomeViewScene.bottomAnchor.constraint(equalTo: layoutguide.bottomAnchor).isActive = true
Remember that if you change constraints on a view programmatically, you must set translatesAutoresizingMaskIntoConstraints to false otherwise you will see no change to that view's position.

UICollectionView not scrolling when stat scrolling from UICollectionViewCell

I'm trying to create dynamic UICollectionView for my application using code below, the problem is that when i start scrolling from CollectionViewCell it's not scrolling but when i start scroll from outside it works fine.
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .horizontal
flowLayout.minimumInteritemSpacing = 10
flowLayout.minimumLineSpacing = 10
let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout)
registerCell(collectionView: collectionView)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.showsVerticalScrollIndicator = false
collectionView.showsHorizontalScrollIndicator = false
_scrollView.addSubview(collectionView)
You can Watch video
CollectionViewCell's properties
Make sure the cells don't have a custom touch-down event (e.g. tap gesture recognizer, or touchesBegan method without calling the super inside) and also make sure the collection view delays content touches, it's a common mistake to uncheck it and forget it.

Why is the search bar pushing the navigation bar out of the picture?

I added a UISearchBar programmatically to my view and every time you activate the searchBar the navigation bar is pushed out of the picture.
My view's hierarchy
searchController.searchBar.delegate = self
navigationItem.title = navigationItem.title ?? ci("plan_p")
tableView.rowHeight = 100.0
tableView.tableHeaderView = searchController.searchBar
tableView.setContentOffset(CGPoint(x: 0.0, y: (self.tableView.tableHeaderView?.frame.size.height)!), animated: false)
guard let projectId = GlobalState.selectedProjectId, let byProject : Results<Structure> = self.by(projectId: projectId) else { return }
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Suche nach Plan"
definesPresentationContext = true
Probably your UITableView is top of the view hierarchy (or more components in top of the view hierarchy).
If your tableView in top of the view hierarchy you should change the tableView's hierarchy in your Interface Builder for solve this issue.
You can simply put your tableView in another UIView component for solve this issue.

Adding searchBar to tableHeaderView adds bottom empty space

I need to add UISearchController to UIViewController I know that the best way to do it is adding it to UINavigationController like this:
let resultSearchController = UISearchController(searchResultsController: athkarSearchTable)
resultSearchController?.searchResultsUpdater = athkarSearchTable
navigationItem.searchController = resultSearchController
The problem is I can't use UINavigationController so I found another solution: adding it to tableHeaderView like the following:
let resultSearchController = ({
let controller = UISearchController(searchResultsController: athkarSearchTable)
controller.searchResultsUpdater = athkarSearchTable
// to give a semi-transparent background when the search bar is selected.
controller.dimsBackgroundDuringPresentation = true
controller.searchBar.sizeToFit()
controller.searchBar.barStyle = UIBarStyle.default
controller.searchBar.searchBarStyle = .minimal
tableView.tableHeaderView = controller.searchBar
return controller
})()
// to limit the overlap area to just the View Controller’s frame instead of the whole Navigation Controller
definesPresentationContext = true
However, this solution adds an empty space to the bottom of the tableView in the UIViewController not the athkarSearchTable which is not good :/ as the screenshot below:
I tried to set the footer to an empty view and to set the height of footer to zero, none of them worked :/
Any suggestion how to remove the bottom empty space?
Problem solved by adding the following lines:
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 400