By presetting the basic attributes of common views, the number of attribute lines that need to be declared each time they are created is reduced (no need to repeat the declaration when they are created later). The bigger the project is, the higher the benefit will be.

🌰 🌰 :

{
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    UIApplication.setupAppearance(.white, barTintColor: .systemBlue)
}
Copy the code

Source:

@objc public extension UIApplication{static func setupAppearance(_ tintColor: UIColor, barTintColor: UIColor) { _ = { $0.barTintColor = barTintColor $0.tintColor = tintColor $0.titleTextAttributes = [NSAttributedString.Key.foregroundColor: tintColor,] }(UINavigationBar.appearance()) _ = { $0.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal) }(UIBarButtonItem.appearance(whenContainedInInstancesOf: [UIImagePickerController.self])) _ = { $0.setTitleColor(tintColor, for: .normal) $0.titleLabel? .adjustsFontSizeToFitWidth = true; $0.titleLabel? . MinimumScaleFactor = 1.0; $0.imageView? .contentMode = .scaleAspectFit $0.isExclusiveTouch = true $0.adjustsImageWhenHighlighted = false }(UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self])) _ = { $0.tintColor = tintColor $0.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: tintColor, ], for: .normal) $0.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: barTintColor, ], for: .selected) }(UISegmentedControl.appearance(whenContainedInInstancesOf: [UINavigationBar.self])) _ = { $0.tintColor = tintColor }(UISegmentedControl.appearance()) _ = { $0.autoresizingMask = [.flexibleWidth, .flexibleHeight] $0.showsHorizontalScrollIndicator = false $0.keyboardDismissMode = .onDrag; # if available (iOS 11.0. *) {$0. ContentInsetAdjustmentBehavior =. Never; } }(UIScrollView.appearance()) _ = { $0.separatorInset = .zero $0.separatorStyle = .singleLine $0.rowHeight = 60 # $0. BackgroundColor =. GroupTableViewBackground if available (iOS 11.0. *) {$0. EstimatedRowHeight = 0.0; $0. EstimatedSectionHeaderHeight = 0.0; $0. EstimatedSectionFooterHeight = 0.0; } }(UITableView.appearance()) _ = { $0.layoutMargins = .zero $0.separatorInset = .zero $0.selectionStyle = .none $0.backgroundColor = .white }(UITableViewCell.appearance()) _ = { $0.scrollsToTop = false $0.isPagingEnabled = true $0.bounces = false }(UICollectionView.appearance()) _ = { $0.layoutMargins = .zero $0.backgroundColor = .white }(UICollectionViewCell.appearance()) _ = { $0.titleLabel? .adjustsFontSizeToFitWidth = true; $0.titleLabel? . MinimumScaleFactor = 1.0; $0.imageView? .contentMode = .scaleAspectFit $0.isExclusiveTouch = true $0.adjustsImageWhenHighlighted = false }(UIButton.appearance()) _ = { $0.isUserInteractionEnabled = true; }(UIImageView.appearance()) _ = { $0.isUserInteractionEnabled = true; }(UILabel.appearance()) _ = { $0.pageIndicatorTintColor = barTintColor $0.currentPageIndicatorTintColor = tintColor $0.isUserInteractionEnabled = true; $0.hidesForSinglePage = true; }(UIPageControl.appearance()) _ = { $0.progressTintColor = barTintColor $0.trackTintColor = .clear }(UIProgressView.appearance()) _ = { $0.datePickerMode = .date; $0.locale = Locale(identifier: "zh_CN"); $0.backgroundColor = .white; # if available (iOS 13.4, *) { $0.preferredDatePickerStyle = .wheels } }(UIDatePicker.appearance()) _ = { $0.minimumTrackTintColor = tintColor $0.autoresizingMask = .flexibleWidth }(UISlider.appearance()) _ = { $0.onTintColor = tintColor $0.autoresizingMask = .flexibleWidth }(UISwitch.appearance()) } }Copy the code