美文网首页
iOS 开发遇到的那些事1

iOS 开发遇到的那些事1

作者: 呵呵先森 | 来源:发表于2020-08-26 22:45 被阅读0次

一. 状态栏

1.黑暗模式切换状态栏的电量栏颜色无法切换问题
    描述:info.plist 已经中已经设置了禁用黑暗模式 但是切换模式后电量等颜色显示有问题
    解决办法:AppDelegate中添加如下代码
     /// 解决 plist 文件禁用黑暗模式不生效问题
    if #available(iOS 13.0, *) {
        UIApplication.shared.statusBarStyle = .darkContent
    } else {
        UIApplication.shared.statusBarStyle = .default
    }
2. 特殊情况状态栏背景色无法修改问题
 ///   解决NavigationBarHidden = true 时候,颜色值无法修改问题
 ///   navigationController?.setNavigationBarHidden(true, animated: animated)
private let statusBar: UIView = UIView()
private func setStatusBarBackgroundColor(color: UIColor) {
    if #available(iOS 13.0, *) {
        statusBar.frame = UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.init(x: 0, y: 0, width: TSScreen.currentWidth, height: 20)
        statusBar.removeFromSuperview()
        statusBar.backgroundColor = color
        UIApplication.shared.keyWindow?.addSubview(statusBar)

    } else {
        let statusBarWindow: UIView = UIApplication.shared.value(forKey: "statusBarWindow") as! UIView
        let statusBar: UIView = statusBarWindow.value(forKey: "statusBar") as! UIView
        if statusBar.responds(to: #selector(setter: UIView.backgroundColor)) {
            statusBar.backgroundColor = color
        }
    }
} 
override func viewWillAppear(_ animated: Bool) {
    setStatusBarBackgroundColor(color: .white)
    navigationController?.setNavigationBarHidden(true, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    statusBar.removeFromSuperview()
}

二. UICollectionView

1.单双排切换
 //  解决单排拉到底部 再切换到双排后 顶部cell 空白的问题
DispatchQueue.main.asyncAfter(deadline: .now()) {
   self.collectionView.reloadData()
}

三.UITableView

1.修改透明tableView的置顶sectionView的背景色

描述: UItabView为透明背景,sectionView也为透明效果, 需求置顶悬停的sectionView需要添加的背景色

 /// 注册headerView
tableView.register(UserSectionView.classForCoder(), forHeaderFooterViewReuseIdentifier: "HeaderView")

/// 必须继承 UITableViewHeaderFooterView
class UserSectionView: UITableViewHeaderFooterView {   }

// 不能用这种方式 设置sectionView
// let sectionView = UserSectionView()
// return sectionView
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {    
    let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderView")
    let sectionView = UserSectionView()
    header?.addSubview(sectionView)
    sectionView.snp.makeConstraints{
        maker in
        maker.edges.equalToSuperview()
    }
    sectionView.backgroundColor = .clear
    sectionView.config(title: pairList[section].title)
    return header
} 
// 置顶sectionView的位置
private var topSectionViewCount = 0

///获取置顶的section位置
private func getSectionHeaderView(){
    var maxIndex = LONG_MAX
    tableView.visibleCells.forEach{ item in
        if let cellSection = tableView.indexPath(for: item)?.section, cellSection < maxIndex {
            maxIndex = cellSection
        }
    }
    topSectionViewCount = maxIndex
} 

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let y = scrollView.contentOffset.y
    let headerHeight = rootView.height
    getSectionHeaderView()
    let topView = tableView.headerView(forSection: topSectionViewCount)

    if y <= headerHeight && y >= 0 {
        // TODO
        return
    }

    if y > headerHeight {
       // TODO
    }
}
2. ios10 UITableViewAutomaticDimension问题

tableview.estimatedRowHeight = UITableViewAutomaticDimension;
在iOS 10.0上所有的约束都失效,
原因为UITableView的估高导致的,在iOS10中,estimatedRowHeight必须要有一个值,否则就会页面默认44

相关文章

网友评论

      本文标题:iOS 开发遇到的那些事1

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