Skip to content

Instantly share code, notes, and snippets.

@shawn-frank
Created February 16, 2022 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawn-frank/927c6bfe33c6c3806d1e3144b49f60fe to your computer and use it in GitHub Desktop.
Save shawn-frank/927c6bfe33c6c3806d1e3144b49f60fe to your computer and use it in GitHub Desktop.
Set up a UITableView to get the default animation when working with a UISearchController like seen in the Apple iOS News app
//
// NewsTableViewVC.swift
// TestApp
//
// Created by Shawn Frank on 16/02/2022.
//
import UIKit
fileprivate class CustomCell: UITableViewCell
{
static let identifier = "CustomCell"
let mainView = UIView()
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")
}
override func layoutSubviews()
{
}
private func setup()
{
mainView.backgroundColor = .gray
mainView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(mainView)
contentView.backgroundColor = .clear
mainView.leadingAnchor
.constraint(equalTo: contentView.leadingAnchor,
constant: 15).isActive = true
mainView.topAnchor
.constraint(equalTo: contentView.topAnchor,
constant: 15).isActive = true
mainView.trailingAnchor
.constraint(equalTo: contentView.trailingAnchor,
constant: -15).isActive = true
mainView.bottomAnchor
.constraint(equalTo: contentView.bottomAnchor,
constant: -15).isActive = true
mainView.layer.cornerRadius = 15
contentView.clipsToBounds = true
}
}
class NewsTableViewVC: UIViewController, UITableViewDataSource, UITableViewDelegate
{
private let searchVC: UISearchController = {
let sc = UISearchController(searchResultsController: nil)
sc.obscuresBackgroundDuringPresentation = true
sc.searchBar.placeholder = "Search"
sc.searchBar.autocapitalizationType = .allCharacters
return sc
}()
private let tableView = UITableView()
override func viewDidLoad()
{
super.viewDidLoad()
// Just to show it's different from the first
view.backgroundColor = .black
setUpNavigationBar()
setUpTableView()
}
private func setUpNavigationBar()
{
//NAVIGATIONBAR:
title = "Weather"
navigationController?.navigationBar.isHidden = true
navigationController?.navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
//NAVIGATION ITEM:
navigationItem.searchController = searchVC
//UISEARCHBARCONTROLLER:
//searchVC.searchResultsUpdater = self
}
private func setUpTableView()
{
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.register(CustomCell.self,
forCellReuseIdentifier: CustomCell.identifier)
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = .clear
view.addSubview(tableView)
// Auto Layout
tableView.leadingAnchor
.constraint(equalTo: view.leadingAnchor,
constant: 0).isActive = true
// This important, configure it to the top of the view
// NOT the safe area margins to get the desired result
tableView.topAnchor
.constraint(equalTo: view.topAnchor,
constant: 0).isActive = true
tableView.trailingAnchor
.constraint(equalTo: view.trailingAnchor,
constant: 0).isActive = true
tableView.bottomAnchor
.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor,
constant: 0).isActive = true
navigationController?.navigationBar.isHidden = false
}
// Just for testing
func randomColor() -> UIColor
{
let red = CGFloat(arc4random_uniform(256)) / 255.0
let blue = CGFloat(arc4random_uniform(256)) / 255.0
let green = CGFloat(arc4random_uniform(256)) / 255.0
return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
}
private func updateSegmentTextColor(_ rootView: UIView)
{
for subview in rootView.subviews
{
if let label = subview as? UILabel
{
// Any color you want
label.textColor = randomColor()
}
updateSegmentTextColor(subview)
}
}
func addControl() {
let items = ["One", "Two", "Three"]
let segmentedControl = UISegmentedControl(items: items)
segmentedControl.frame = CGRect(x: 35, y: 200, width: 250, height: 50)
segmentedControl.center = view.center
segmentedControl.selectedSegmentIndex = 1
segmentedControl.tintColor = .blue
view.addSubview(segmentedControl)
// custom function
updateSegmentTextColor(segmentedControl)
}
func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int
{
return 15
}
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell
= tableView.dequeueReusableCell(withIdentifier: CustomCell.identifier)
?? UITableViewCell()
return cell
}
func tableView(_ tableView: UITableView,
heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 150
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment