I have a UIViewController with a navigation bar and a tab bar. Other than that, the whole screen is made up of a UITableView.
I have a large tableHeaderView that has the same background color as the navbar.
When I drag the content up (scrolling down) everything looks fine.
But if I drag it up, there is an ugly disconnection between the navigation bar and the header view.
Is there any way I could anchor it to the top when dragging down, while allowing it to scroll when dragging up?
You can try creating a view and placing it behind the tableView, as the table view scrolls, the height of the view is updated.
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
lazy var tableView : UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.dataSource = self
tableView.delegate = self
return tableView
}()
let backView : UIView = {
let view = UIView()
view.backgroundColor = .red
return view
}()
var backViewHeight : NSLayoutConstraint?
override func viewDidLoad() {
super.viewDidLoad()
self.title = "ViewController"
self.view.addSubview(backView)
backView.translatesAutoresizingMaskIntoConstraints = false
backView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
backView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
backView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
backViewHeight = backView.heightAnchor.constraint(equalToConstant: 0)
backViewHeight?.isActive = true
self.view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
tableView.register(Cell.self, forCellReuseIdentifier: "cell")
tableView.register(Header.self, forHeaderFooterViewReuseIdentifier: "header")
tableView.backgroundColor = .clear
self.navigationController?.navigationBar.barTintColor = .red
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y < 0 {
backViewHeight?.constant = -scrollView.contentOffset.y
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header")
header?.contentView.backgroundColor = .red
let headerLabel = UILabel(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 100))
headerLabel.textAlignment = .center
headerLabel.text = "Header"
header?.addSubview(headerLabel)
return header
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let view = UIView()
view.backgroundColor = .white
return view
}
}
class Cell: UITableViewCell {
let label : UILabel = {
let label = UILabel()
label.text = "One Label"
return label
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.backgroundColor = .clear
setupViews()
}
func setupViews() {
self.backgroundColor = .white
self.addSubview(label)
label.frame = self.frame
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class Header : UITableViewHeaderFooterView {
override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
If you copy paste this code in an empty project you can have a look at the behavior. Don't forget to embed the ViewController in a NavigationController. Hope it helps
1) If an unwanted white space on the top of tableview is permanent and the constraints are correct this is the solution.
the scroll view insets adjusted automatically if you disable it should remove it
if #available(iOS 11.0, *) {
tableView.contentInsetAdjustmentBehavior = .never
} else {
automaticallyAdjustsScrollViewInsets = false
}
2) if u just have it when u pull down and it goes back to its normal state. It means is that the tableview bouncing is enabled and that is normal behaviour according to iOS documentation:
If the value of this property is true, the scroll view bounces when it encounters a boundary of the content. Bouncing visually indicates that scrolling has reached an edge of the content. If the value is false, scrolling stops immediately at the content boundary without bouncing. The default value is true.
you can uncheck the bouncing from the tableview in your storyboard or xib file. Or u can use this snippet:
tableView.bounces = false
tableView.alwaysBounceVertical = false
Note: that is not recommended to disable the scroll bouncing since it would make things feel very unnatural for iOS.
and also if you want to use pull to refresh it will not work.
So finally if u choose to not disable it you will have to change the background color of the parent of your tableview and it will solve it.
I hope that makes sense!
Related
I am trying to use SwiftUI view inside UITableViewCell that is working fine until iOS 15. On iOS 15 when I scroll table view and once initial visible cells go off screen and new cells being reused those are having extra height added.
Following is the source code.
import UIKit
import SwiftUI
class ViewController: UIViewController {
#IBOutlet weak var tableView: UITableView?
override func viewDidLoad() {
super.viewDidLoad()
tableView?.register(TableViewCell.self, forCellReuseIdentifier: "cell")
tableView?.rowHeight = UITableView.automaticDimension
tableView?.estimatedRowHeight = 100
tableView?.separatorColor = .white
tableView?.dataSource = self
}
}
extension ViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
20
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? TableViewCell
return cell ?? UITableViewCell()
}
}
class TableViewCell: UITableViewCell {
var stackView: UIStackView = {
let stack = UIStackView(frame: .zero)
stack.axis = .vertical
return stack
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupView()
}
func setupView() {
contentView.clipsToBounds = true
contentView.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
stackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
stackView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
let hosting = UIHostingController(rootView: TestView())
stackView.addArrangedSubview(hosting.view)
hosting.view.backgroundColor = .red
}
}
struct TestView: View {
var body: some View {
Rectangle()
.fill(Color.green)
.frame(height: 100)
}
}
Any help will be highly appreciated. Thanks
I have a weird requirement for UI, and need to scroll a table view in a page view using the scroll view I embed the page view in. For example:
import UIKit
class TableVC: UIViewController {
let tableView = UITableView()
var observer: NSKeyValueObservation?
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
}
func setupTableView() {
view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
tableView.topAnchor.constraint(equalTo: view.topAnchor),
tableView.heightAnchor.constraint(equalToConstant: 2000)
])
tableView.dataSource = self
tableView.delegate = self
tableView.isScrollEnabled = false
tableView.backgroundColor = .blue
let handler = { (tableView: UITableView, change: NSKeyValueObservedChange<CGSize>) in
if let contentSize = change.newValue {
print("contentSize:", contentSize)
}
}
observer = tableView.observe(\UITableView.contentSize, options: [NSKeyValueObservingOptions.new], changeHandler: handler)
}
}
extension TableVC: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
50.0
}
}
extension TableVC: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 40
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.backgroundColor = .green
return cell
}
}
And the main view controller:
import UIKit
class ViewController: UIViewController {
private enum Constants {
static let pageViewControllerOptions: [UIPageViewController.OptionsKey: Any] = [
.interPageSpacing: CGFloat(8.0)
]
}
private let pageVC: UIPageViewController = UIPageViewController(
transitionStyle: .scroll,
navigationOrientation: .horizontal,
options: Constants.pageViewControllerOptions
)
let scrollView: UIScrollView = UIScrollView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(scrollView)
scrollView.pin(to: view)
let stackView = UIStackView()
stackView.axis = .vertical
stackView.frame = view.bounds
stackView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
scrollView.addSubview(stackView)
let blueView = UIView()
blueView.backgroundColor = .blue
stackView.addArrangedSubview(blueView)
blueView.translatesAutoresizingMaskIntoConstraints = false
blueView.heightAnchor.constraint(equalToConstant: 100.0).isActive = true
addChild(pageVC)
pageVC.didMove(toParent: self)
let pageView = pageVC.view!
stackView.addArrangedSubview(pageView)
pageVC.setViewControllers([TableVC()], direction: .forward, animated: false, completion: nil)
// 50 height with 20 cells works fine
// 50 height with 40 cells won't work beacuse it doesn't seem to load them all
scrollView.contentSize = CGSize(width: view.bounds.width, height: 2000 + 100)
}
}
With 40 cells I am expecting a content size of 2000, I set this above (allowing for the height of the blue view also). The scrolling works but it seems like at some point it has stopped generating the cells:
In the gif above you can see there are less than 40 cells. I have tried using a larger table view height, calling layoutIfNeeded but the result is the same. Is there anything I can do to fix this? I imagine the cells are not loading because I am not using the table view's own scroll view so cellForRow isn't triggered. Is there another way to load the cells?
I am trying to build a TableView programmatically, but I cannot get a basic standard label to display; all I see is basic empty cells. Here's my code:
TableView Cell:
class TableCell: UITableViewCell {
let cellView: UIView = {
let view = UIView()
view.backgroundColor = .systemRed
return view
}()
let labelView: UILabel = {
let label = UILabel()
label.text = "Cell 1"
return label
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setup()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setup() {
addSubview(cellView)
NSLayoutConstraint.activate([
cellView.topAnchor.constraint(equalTo: topAnchor),
cellView.bottomAnchor.constraint(equalTo: bottomAnchor),
cellView.leadingAnchor.constraint(equalTo: leadingAnchor),
cellView.trailingAnchor.constraint(equalTo: trailingAnchor)])
cellView.addSubview(labelView)
}
}
Data Source:
class TableDataSource: NSObject, UITableViewDataSource {
let cellID = "cell"
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! TableCell
return cell
}
}
And this is the VC:
class TableViewController: UITableViewController {
let dataSource = TableDataSource()
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(TableCell.self, forCellReuseIdentifier: dataSource.cellID)
tableView.dataSource = dataSource
}
}
I am trying to keep the code as basic as possible for future references. I've set various breakpoints to see what could go wrong, but they all check out. Could it be the constraints that are wrong?
Any help is appreciated.
I see several errors in your cell.
Add subviews to contentView, not directly to cell:
contentView.addSubview(cellView)
cellView.addSubview(labelView)
The same is necessary for constraints:
NSLayoutConstraint.activate([
cellView.topAnchor.constraint(equalTo: contentView.topAnchor),
cellView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
cellView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
cellView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor)
])
Views created in code need to set translatesAutoresizingMaskIntoConstraints = false,
let cellView: UIView = {
let view = UIView()
view.backgroundColor = .systemRed
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let labelView: UILabel = {
let label = UILabel()
label.text = "Cell 1"
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
There are no constraints for your label.
Your constraints don't work, because you need to change translatesAutoresizingMaskIntoConstraints for cellView in your setup():
func setup() {
addSubview(cellView)
cellView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
cellView.topAnchor.constraint(equalTo: topAnchor),
cellView.bottomAnchor.constraint(equalTo: bottomAnchor),
cellView.leadingAnchor.constraint(equalTo: leadingAnchor),
cellView.trailingAnchor.constraint(equalTo: trailingAnchor)])
cellView.addSubview(labelView)
}
UPD Resolved - see edited question below
Trying to add a custom view (button, to be more specific) to a custom subclass of UITableViewCell but unable to see any layout results on iOS 10.1 on the device. Did not see any changes on the layout, and tried to just fill the cell with a custom view with the red background but failed to achieve this result as well.
import UIKit
class Save: UITableViewCell {
let containerView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .red
return view
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews() {
self.translatesAutoresizingMaskIntoConstraints = false
addSubview(containerView)
containerView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
containerView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
containerView.topAnchor.constraint(equalTo: topAnchor).isActive = true
containerView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
}
}
What I tried besides that: using plain addSubiew and constraint to the self anchors of the cell, and add to contentView in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) but both approaches did nothing, the cell appears but it is empty.
UPD
attaching the TableView initiation code for the reference
class SettingsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let settings = [
"setting1",
"setting2",
"setting3",
"setting4",
"setting5",
"setting6",
"setting7"
]
let overlay: UIView = {
var view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .gray
view.alpha = 0.3
view.isHidden = true
return view
}()
let tableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .grouped)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "commonCell")
tableView.register(Save.self, forCellReuseIdentifier: "btnCell")
tableView.register(Switcher.self, forCellReuseIdentifier: "switcherCell")
tableView.headerView(forSection: 0)?.textLabel?.text = "settings of app"
tableView.tableFooterView = UIView()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = UITableViewAutomaticDimension
tableView.allowsSelection = false
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
func setupViews() {
view.backgroundColor = .white
view.addSubview(tableView)
tableView.dataSource = self
tableView.delegate = self
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
tabBarController?.navigationItem.rightBarButtonItems = []
tabBarController?.navigationItem.title = "settings"
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return settings.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "settings"
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "commonCell", for: indexPath)
cell.textLabel?.text = settings[indexPath.row]
if(indexPath.row == 6) {
cell.textLabel?.text = ""
cell = tableView.dequeueReusableCell(withIdentifier: "btnCell", for: indexPath)
}
return cell
}
}
RESOLVED
in addition to Sh_Khan answer, it was required to set
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 65
on the table
It doesn't look like it's anything wrong with the code in your cell class in my eyes. Can you please add your tableviewcode?
You should either implement
heightForRowAt
Or add
containerView.heightAnchor.constraint(equalToConstant:200).isActive = true
Also add any subview to contentView
contentView.addSubview(containerView)
NSLayoutConstraint.activate([
containerView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
containerView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
containerView.topAnchor.constraint(equalTo: contentView.topAnchor),
containerView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
containerView.heightAnchor.constraint(equalToConstant:200)
])
I want to adjust my table view height so that it shows only x cells at a time. The cell's layout is configurable so I don't know its height beforehand.
For simplicity's sake, I've tried to set up the simplest example of a cell with two labels with fixed text stacked vertically.
MyCustomViewCell.swift
class MyCustomViewCell: UITableViewCell {
let label: UILabel
let label2: UILabel
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
label = UILabel()
label2 = UILabel()
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.contentView.addSubview(label)
self.contentView.addSubview(label2)
label.translatesAutoresizingMaskIntoConstraints = false
label.topAnchor.constraint(equalTo: self.contentView.topAnchor).isActive = true
label.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor).isActive = true
label.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true
label2.translatesAutoresizingMaskIntoConstraints = false
label2.topAnchor.constraint(equalTo: label.bottomAnchor).isActive = true
label2.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true
label2.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor).isActive = true
label2.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true
label.text = "string 1"
label2.text = "string 2"
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
I have managed to achieve the desired behavior by setting the height constraint (with a lower than 1000 priority) when initializing the table view and then setting the actual height on cellForRowAtIndexPath based on the cell's frame and the amount of cells I wanted to be visible (in this case, 5).
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let cellIdentifier = "cellIdentifier"
override func viewDidLoad() {
super.viewDidLoad()
let tableView = UITableView(frame: .zero, style: .plain)
setupTableViewConstraints(tableView: tableView)
tableView.register(MyCustomViewCell.self, forCellReuseIdentifier: cellIdentifier)
tableView.dataSource = self
tableView.delegate = self
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func setupTableViewConstraints(tableView: UITableView) {
self.view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50).isActive = true
let heightConstraint = tableView.heightAnchor.constraint(equalToConstant: 300)
heightConstraint.priority = 900
heightConstraint.isActive = true
tableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 10).isActive = true
tableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -10).isActive = true
tableView.layer.borderColor = UIColor.black.cgColor
tableView.layer.borderWidth = 0.5
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10000
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath as IndexPath) as! MyCustomViewCell
tableView.heightAnchor.constraint(equalToConstant: cell.frame.height*5).isActive = true
return cell
}
}
However, this approach doesn't seem right to me. All the way from setting a "dummy" height only to be adjusted later to setting the table view height every time cellForRowAtIndexPath is executed.
Can anyone point me in the direction of a more elegant solution?
In your example you are updating the height of your tableview every time it asks you to load a cell.
While this would work, it seems a bit overkill knowing that this method is constantly called by the tableview even before the cells are visible.
Why not wait until the tableview has finished loading and only then update the height?
You could do this in willDisplayCell instead which is trigged only when the cells are about to be displayed or perhaps with a completion closure like so:
self.tableView.reloadData()
DispatchQueue.main.async {
// This closure is only called when the data has been reloaded
// and thus will enable you to calculate the final content height
}