swift开发中小问题解决方案

  1. 沉浸式导航

    第一种方式,直接在 viewDidLoad 方法中设置,这种方式我用了,出现一个问题,A 页面跳转到 B 页面后,再点击返回,A 页面的导航栏背景颜色会受到 B 页面的影响,导致颜色和位置都出现问题

    //将背景栏设置为不透明
    self.navigationController?.navigationBar.isTranslucent = false
    // 去掉横线
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(),for: .default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    

    第二种方式,是通过重写 viewWillAppear 和 viewWillDisappear 方法实现的,页面相互跳转基本没有问题

    // 视图将要展示出来
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        // 设置导航栏透明
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
    }
    
    // 视图将要消失
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        // 重置导航栏背景
        self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
        self.navigationController?.navigationBar.shadowImage = nil
    }
    
  2. collectionView 重新定位到页面顶部

    // 第一种方式
    self.collectionView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
    
  3. 数据下拉加载

    // 定义存放变量的类型为 
    var data: NSMutableArray = NSMutableArray.init()
    // 如果返回数据结构是
    var rundata: NSArray = NSArray.init()	
    // 可以直接通过以下方法,把数据追加后面
    self.data.addObjects(from: rundata as! [Any])
    
  4. 横向虚线函数

    import Foundation
    
    class ViewUtility {
        
        /// 获取横向虚线视图
        /// - Parameters:
        ///   - rootview: 页面父级视图
        ///   - topview: 顶部视图
        ///   - leftmargin: 距离左边
        ///   - topmargin: 距离上边
        ///   - linewidth: 横线宽度
        ///   - lineheight: 横线高度
        ///   - backgroundcolor: 横线颜色
        /// - Returns: 返回横向虚线视图,方便下面的视图,以横线做参照物继续处理视图
        static func addDottedLineView(rootview: UIView, topview: UIView, leftmargin: CGFloat, topmargin: CGFloat, linewidth: CGFloat, lineheight: CGFloat = 1~, backgroundcolor: UIColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1)) -> UIView {
            let hengxianView = UIView()
            hengxianView.backgroundColor = backgroundcolor
            rootview.addSubview(hengxianView)
            hengxianView.translatesAutoresizingMaskIntoConstraints = false
            UIUtility.setviewwidthandheightconstraint(parentview: rootview, item: hengxianView, width: linewidth, height: lineheight)
            UIUtility.setviewleftleftconstraint(parentview: rootview, item: hengxianView, leftmargin: leftmargin)
            let hengxianViewTop = UIUtility.gettopmarginconstraint(item: hengxianView, toitem: topview, topmargin: topmargin)
            UIUtility.setConstrains(rootview: rootview, constrains: hengxianViewTop)
            return hengxianView
        }
      
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值