美文网首页
iOS常用值和常用方法

iOS常用值和常用方法

作者: Eyes_cc | 来源:发表于2019-10-30 16:30 被阅读0次

    状态栏的高度 (self.navigationController?.navigationBar.frame.size.height)!
    导航栏的高度 UIApplication.shared.statusBarFrame.size.height

    常用方法

    UIView.animate动画和snapkit的搭配使用,self.view.layoutIfNeeded()很关键。这里的view指的是进行动画的控件的直接父视图

    #写法1:
    UIView.animate(withDuration: 0.3) {
        self.promptSuggestOrdersView.snp.remakeConstraints { (make) in
             make.left.top.right.equalToSuperview()
             make.height.equalTo(40)
        }
        self.tableView.snp.remakeConstraints { (make) in
             make.top.equalTo(self.promptSuggestOrdersView.snp.bottom)
             make.left.right.equalToSuperview()
             make.bottom.equalTo(self.bottomBar.snp.top)
        }
        self.view.layoutIfNeeded()
     }
    #写法2:
    self.promptSuggestOrdersView.snp.remakeConstraints { (make) in
         make.left.top.right.equalToSuperview()
         make.height.equalTo(40)
    }
    self.tableView.snp.remakeConstraints { (make) in
         make.top.equalTo(self.promptSuggestOrdersView.snp.bottom)
         make.left.right.equalToSuperview()
         make.bottom.equalTo(self.bottomBar.snp.top)
    }
    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
    #写法3:
    self.promptSuggestOrdersView.snp.remakeConstraints { (make) in
         make.left.top.right.equalToSuperview()
         make.height.equalTo(40)
    }
    self.tableView.snp.remakeConstraints { (make) in
         make.top.equalTo(self.promptSuggestOrdersView.snp.bottom)
         make.left.right.equalToSuperview()
         make.bottom.equalTo(self.bottomBar.snp.top)
    }
    UIView.animate(withDuration: 0.3, animations: {
        self.bgView.layoutIfNeeded()
        }, completion:nil)
    #写法4: 当整个视图是在 scrollView 上的时候,为了适配 contentSize 空间,此时
    UIView.animate(withDuration: 0.3, animations: {
        self.bgView.snp.remakeConstraints { (make) in
            make.top.equalTo(self.oldShop.snp.bottom).offset(25)
            make.leading.trailing.equalTo(self.view).inset(20)
            make.height.equalTo(300)
        }
        self.bgViewTwo.snp.remakeConstraints { (make) in
            make.left.right.equalTo(self.view).inset(20)
            make.top.equalTo(self.bgView.snp.bottom).offset(15)
            make.height.equalTo(0)
        }
        self.view.layoutIfNeeded()
        self.scrollView.contentSize.height = self.nextLogin.frame.maxY+100
    })
    

    给View视图添加部分圆角,需要注意的是使用贝塞尔曲线时要在视图布局以后才有效果。

    override func viewDidLayoutSubviews() {
            cardView.roundCorners(cornerRadius: 20.0, corners: [.topLeft, .topRight])
        }
        
    }
    extension UIView {
        // 使用贝塞尔曲线添加部分圆角。 .allCorners/[.topLeft, .topRight]
        func roundCorners(cornerRadius: Double, corners: UIRectCorner) {
            let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
            let maskLayer = CAShapeLayer()
            maskLayer.frame = self.bounds  // 可有可无
            maskLayer.path = path.cgPath
            self.layer.mask = maskLayer
        }
        // 使用贝塞尔曲线将所有角设置为圆角
        func roundCorners(cornerRadius: CGFloat) {
            let path = UIBezierPath(roundedRect: self.bounds, cornerRadius: cornerRadius)
            let maskLayer = CAShapeLayer()
            maskLayer.frame = self.bounds  // 可有可无
            maskLayer.path = path.cgPath
            self.layer.mask = maskLayer
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS常用值和常用方法

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