How to get rid of an empty UITableCell appearing top and in between tableview - swift4

I used below lines of code to get rid of the empty UITableview cells but no luck. Any help is appreciated.
tableView.tableFooterView = UIView()
tableView.tableHeaderView = UIView()

You should use nil instead of UIView()
tableView.tableHeaderView = nil

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
}

How to display a UITextView programmatically?

I am trying to display a basic UITextView programmatically. The problem is, if I want to display a UILabel with the same constraints and settings, it's all fine, I can have it displayed. However, if I change it to a UITextView, it just disappears. I created an empty project, just to check it, but still, no avail, just does not display.
Here's my code that works fine for a UILabel:
view = UIView()
view.backgroundColor = .systemBackground
explanationView = UILabel()
explanationView.layer.zPosition = 1
explanationView.translatesAutoresizingMaskIntoConstraints = false
explanationView.numberOfLines = 10
view.addSubview(explanationView)
NSLayoutConstraint.activate([
explanationView.centerXAnchor.constraint(equalTo: view.layoutMarginsGuide.centerXAnchor),
explanationView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor),
explanationView.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),
explanationView.bottomAnchor.constraint(greaterThanOrEqualTo: view.layoutMarginsGuide.bottomAnchor, constant: -20)
])
I am using layer.zPosition to display it over a UIImageView. I also add .isScrollEnabled for the text view. I also tried creating the UITextView via a computed property, but did not have any luck either.
Any help is appreciated.
Edit: Below is the code that I use for the text view.
explanationView = UITextView()
explanationView.layer.zPosition = 1
explanationView.translatesAutoresizingMaskIntoConstraints = false
explanationView.isScrollEnabled = true
view.addSubview(explanationView)

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

Can't get rid of a UIviewcontroller header

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;

Swift. How to add a UIView on top of a GMSPanoramaView

I have a simple GMSPanoramaView and I want to add a UIView on top of it, I've tryed whith addSubview and changing the layers zposition but I't dosen't show up.
Any idea's on how I can do it?
The way I implemented the GMSPanoramaView:
let panoView = GMSPanoramaView(frame: CGRectZero)
self.view = panoView
panoView.moveNearCoordinate(CLLocationCoordinate2DMake(latitude, longitude))
Try this:
Approach 1:
var yourView = UIView()
self.view.bringSubviewToFront(yourView)
Approach 2:
var gmsView = GMSPanoramaView()
self.view.addSubview(gmsView)
var anotherView = UIView()
self.view.insertSubview(anotherView, aboveSubview: gmsView)
This should work!!