To connect a diffable data source to a table view(iOS 13 and onward)
lazy vardataSource=configureDataSource()overridefunc viewDidLoad(){
super.viewDidLoad()//Assign the diffable data source to your table view.
tableView.dataSource = dataSource
//Generate the current state of the table data by creating a snapshotvarsnapshot=NSDiffableDataSourceSnapshot<Section,String>()
snapshot.appendSections([.all])
snapshot.appendItems(restaurantNames, toSection:.all)//Call the apply() function of the data source to populate the data
dataSource.apply(snapshot, animatingDifferences: false)}func configureDataSource()->UITableViewDiffableDataSource<Section,String>{letcellIdentifier="favoritecell"//Create a UITableViewDiffableDataSource object to connect with your table andprovide the configuration of the table view cells.letdataSource=UITableViewDiffableDataSource<Section,String>(
tableView: tableView,
cellProvider:{ tableView, indexPath, restaurantName inletcell= tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)as!RestaurantTableViewCell
cell.nameLabel.text = restaurantName
cell.thumbnailImageView.image =UIImage(named:self.restaurantImages[indexPath.row])
cell.locationLabel.text =self.restaurantLocations[indexPath.row]
cell.typeLabel.text =self.restaurantTypes[indexPath.row]
cell.heartMark.isHidden = !self.restaurantIsFavorites[indexPath.row]
cell.heartMark.tintColor =UITraitCollection.isDarkMode ? .systemYellow :.blue
return cell
})return dataSource
}
overridefunc traitCollectionDidChange(_ previousTraitCollection:UITraitCollection?){
super.traitCollectionDidChange(previousTraitCollection)// perform action here when user changes the text size
switch self.traitCollection.preferredContentSizeCategory {case.extraExtraLarge,.extraExtraExtraLarge,.accessibilityExtraLarge,.accessibilityExtraExtraLarge,.accessibilityExtraExtraExtraLarge:self.isDynamicLargeType = true
case.extraSmall,.small,.medium,.accessibilityMedium,.large,.extraLarge:self.isDynamicLargeType = false
default:self.isDynamicLargeType = false
}// reload view here when user changes the text size and change cell identifier
if previousTraitCollection?.preferredContentSizeCategory != traitCollection.preferredContentSizeCategory {self.dataSource =configureDataSource()self.viewDidLoad()}}
Set Transparent navigation bar
Before
Before
After
After
overridefunc viewDidLoad(){
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.hidesBarsOnSwipe = true
if #available(iOS 14.0,*){
navigationItem.backButtonDisplayMode =.minimal
}else{
navigationItem.backButtonTitle =""}// To customize the navigation bar, you first need to retrieve the currentUINavigationBarAppearance object// The standardAppearance property contains thecurrent appearance settings for the standard size navigation bar
if let appearance = navigationController?.navigationBar.standardAppearance {
appearance.configureWithTransparentBackground()
if let customFont =UIFont(name:"Nunito-Bold", size: 40.0){
appearance.titleTextAttributes =[.foregroundColor:UIColor(named:"NavigationBarTitle")!,.font: customFont]
appearance.largeTitleTextAttributes =[.foregroundColor:UIColor(named:"NavigationBarTitle")!,.font: customFont]}
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.compactAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
}func application(_ application:UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey:Any]?)->Bool{letnavBarAppearence=UINavigationBarAppearance()varbackButtonImage=UIImage(systemName:"arrow.backword", withConfiguration:UIImage.SymbolConfiguration(pointSize: 20.0, weight:.bold))
backButtonImage = backButtonImage?.withAlignmentRectInsets(UIEdgeInsets(top:0, left:-10, bottom:0, right:0))UINavigationBar.appearance().tintColor =.white
UINavigationBar.appearance().standardAppearance = navBarAppearence
UINavigationBar.appearance().compactAppearance = navBarAppearence
UINavigationBar.appearance().scrollEdgeAppearance = navBarAppearence
return true
}
To prevent content from becoming overly wide for iPad