美文网首页
Swift 重构:通过预设视图样式,缩减代码量

Swift 重构:通过预设视图样式,缩减代码量

作者: SoaringHeart | 来源:发表于2021-06-16 18:34 被阅读0次

    通过预设常用视图基础属性,缩减每次创建时需要声明的属性行数(之后创建时不需要再重复声明),项目越大收益越高;

    🌰🌰:

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

    源码:

    @objc public extension UIApplication{
    
        /// 配置 app 外观主题色
        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())
            
        }
    }
    

    相关文章

      网友评论

          本文标题:Swift 重构:通过预设视图样式,缩减代码量

          本文链接:https://www.haomeiwen.com/subject/rnsfyltx.html