-
沉浸式导航
第一种方式,直接在 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 }
-
collectionView 重新定位到页面顶部
// 第一种方式 self.collectionView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
-
数据下拉加载
// 定义存放变量的类型为 var data: NSMutableArray = NSMutableArray.init() // 如果返回数据结构是 var rundata: NSArray = NSArray.init() // 可以直接通过以下方法,把数据追加后面 self.data.addObjects(from: rundata as! [Any])
-
横向虚线函数
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 } }
swift开发中小问题解决方案
最新推荐文章于 2022-01-26 10:06:38 发布