You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To connect a diffable data source to a table view(iOS 13 and onward)
lazy vardataSource=configureDataSource()overridefuncviewDidLoad(){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 snapshot
varsnapshot=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)}funcconfigureDataSource()->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,restaurantNameinletcell=tableView.dequeueReusableCell(withIdentifier:cellIdentifier,for:indexPath)as!RestaurantTableViewCellcell.nameLabel.text=restaurantNamecell.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:.bluereturncell})returndataSource}
overridefuncviewDidLoad(){super.viewDidLoad()letmoveScaleTransform=getScaleAnimation()
// Make the button invisible
forrateButtoninrateButtons{rateButton.transform=moveScaleTransformrateButton.alpha=0}}funcgetScaleAnimation()->CGAffineTransform{letmoveRightTransform=CGAffineTransform.init(translationX:600,y:0)letscaleUpTransform=CGAffineTransform.init(scaleX:5.0,y:5.0)returnscaleUpTransform.concatenating(moveRightTransform)}funcsetSpringAnimation(){vardelay:TimeInterval=0.1forrateButtoninself.rateButtons{UIView.animate(withDuration:0.8,delay:delay,usingSpringWithDamping:0.2,initialSpringVelocity:0.3,options:[],animations:{rateButton.alpha=1.0rateButton.transform=.identity},completion:nil)delay+=0.1}UIView.animate(withDuration:0.4,delay:0.1,options:[],animations:{self.closeButton.alpha=1.0},completion:nil)}funcsetStandardAnimation(){vardelay:TimeInterval=0.1forrateButtoninself.rateButtons{UIView.animate(withDuration:0.4,delay:delay,options:[],animations:{rateButton.alpha=1.0rateButton.transform=.identity},completion:nil)delay+=0.1}UIView.animate(withDuration:0.4,delay:0.1,options:[],animations:{self.closeButton.alpha=1.0},completion:nil)}
overridefunctraitCollectionDidChange(_previousTraitCollection:UITraitCollection?){super.traitCollectionDidChange(previousTraitCollection)
// perform action here when user changes the text size
switchself.traitCollection.preferredContentSizeCategory{case.extraExtraLarge,.extraExtraExtraLarge,.accessibilityExtraLarge,.accessibilityExtraExtraLarge,.accessibilityExtraExtraExtraLarge:self.isDynamicLargeType=truecase.extraSmall,.small,.medium,.accessibilityMedium,.large,.extraLarge:self.isDynamicLargeType=falsedefault:self.isDynamicLargeType=false}
// reload view here when user changes the text size and change cell identifier
ifpreviousTraitCollection?.preferredContentSizeCategory!=traitCollection.preferredContentSizeCategory{self.dataSource=configureDataSource()self.viewDidLoad()}}
Set Transparent navigation bar
Before
Before
After
After
overridefuncviewDidLoad(){super.viewDidLoad()navigationController?.navigationBar.prefersLargeTitles=truenavigationController?.hidesBarsOnSwipe=trueif#available(iOS14.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
ifletappearance=navigationController?.navigationBar.standardAppearance{appearance.configureWithTransparentBackground()ifletcustomFont=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=appearancenavigationController?.navigationBar.compactAppearance=appearancenavigationController?.navigationBar.scrollEdgeAppearance=appearance}funcapplication(_application:UIApplication,didFinishLaunchingWithOptionslaunchOptions:[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=.whiteUINavigationBar.appearance().standardAppearance=navBarAppearenceUINavigationBar.appearance().compactAppearance=navBarAppearenceUINavigationBar.appearance().scrollEdgeAppearance=navBarAppearencereturntrue}
To prevent content from becoming overly wide for iPad
//-------acronym--------------
// Called when the iOS interface environment changes.
overridefunctraitCollectionDidChange(_previousTraitCollection:UITraitCollection?){super.traitCollectionDidChange(previousTraitCollection)self.heartMark.tintColor=UITraitCollection.isDarkMode?.systemYellow:.systemPink}
//---------------------
extensionUITraitCollection{publicstaticvarisDarkMode:Bool{if#available(iOS13,*),current.userInterfaceStyle==.dark{returntrue}returnfalse}}