Swift常用控件集合

1.UIButton

        let btn = UIButton()
        btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        btn.layer.cornerRadius = 10.P
        btn.backgroundColor = .yellow
        btn.setTitle("点击一下", for: .normal)
        btn.setTitleColor(kRGB(51, 51, 51), for: .normal)
        btn.titleLabel?.font = kFont(14)
        self.addSubview(btn)
        btn.snp.makeConstraints { make in
            make.left.equalTo(self).offset(P(10))
            make.right.equalTo(self).offset(-10.P)
            make.width.equalTo(10.P)
            make.height.equalTo(10.P)
        }

2.UILabel

        let titleLabel = UILabel()
        titleLabel.text = "隐私协议"
        titleLabel.font = kBFont(16)
        titleLabel.textColor = kRGB(51, 51, 51)
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints {(make) in
            make.centerY.equalTo(iconImgV)
            make.height.equalTo(16.P)
            make.left.equalTo(iconImgV.snp.right).offset(6.P)
            make.width.equalTo(70.P)
        }

3.UIImageView

		let imgV = UIImageView()
        imgV.image = UIImage(named: "")
        self.addSubview(imgV)
        imgV.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(10.P)
            make.right.equalTo(self).offset(10.P)
            make.width.equalTo(100.P)
            make.height.equalTo(100.P)
        }

4.UITextField

        let textField = UITextField()
        textField.text = "输入"
        textField.placeholder = "轻呼入"
        textField.font = kFont(14)
        textField.textColor = .red
        textField.backgroundColor = .yellow
        textField.addTarget(self, action: #selector(textFieldChange(_:)), for: .editingChanged)
        self.addSubview(textField)
        textField.snp.makeConstraints {(make) in
            make.left.equalTo(self).offset(10.P)
            make.right.equalTo(self).offset(-10.P)
            make.width.equalTo(100.P)
            make.height.equalTo(100.P)
        }
        
        @objc private func textFieldChange(_ textField: UITextField) {
            guard let _: UITextRange = textField.markedTextRange else{
                if (textField.text! as NSString).length > 5 {
                    textField.text = (textField.text! as NSString).substring(to: 5)
                }
                return
            }
        }

5.UITableView

    private func setupViews() {
        
        let tableview = UITableView()
        tableview.backgroundColor = .gray
        tableview.delegate = self
        tableview.dataSource = self
        tableview.separatorStyle = .none
        tableview.register(HomeTableCell.self, forCellReuseIdentifier: "cell")
        tableview.mj_footer = MJRefreshBackNormalFooter.init(refreshingTarget: self, refreshingAction: #selector(refreshBottom))
        tableview.mj_header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(refreshTop))
        self.view.addSubview(tableview)
        tableview.snp.makeConstraints { (make) in
            make.top.equalTo(self.view)
            make.left.right.equalTo(self.view)
            make.bottom.equalTo(self.view)
        }
    }
    
    @objc private func refreshTop() {
        
    }
    
    @objc private func refreshBottom() {
        
    }
}
    
extension ViewController : UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100.P
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var model = People.init()
        model.name = "hhhhhhhhhhhh"
        let cell : HomeTableCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HomeTableCell
        cell.model = model
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}

6.UICollectionView

    private func setupViews() {
        
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize(width: 100.P, height: 100.P)
        layout.minimumLineSpacing = 10.P
        layout.minimumInteritemSpacing = 10.P
        layout.scrollDirection = .vertical
        
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .gray
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(CollecCell.self, forCellWithReuseIdentifier: "cell")
        collectionView.register(CollectionHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "header")
        collectionView.register(CollectionFootView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "foot")
        collectionView.mj_header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(refreshTop))
        collectionView.mj_footer = MJRefreshBackNormalFooter.init(refreshingTarget: self, refreshingAction: #selector(refreshBottom))
        self.view.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(self.view)
            make.left.right.equalTo(self.view)
            make.bottom.equalTo(self.view)
        }
    }

    @objc func refreshTop() {
        
    }

    @objc func refreshBottom() {
        
    }
}



extension HomeVC : UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 30
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: CollecCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollecCell
        return cell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10.P, left: 10.P, bottom: 10.P, right: 10.P)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: 0, height: 200.P)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize(width: 0, height: 200.P)
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader  {
            let headerView:CollectionHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath) as! CollectionHeaderView
            return headerView
        } else {
            let footerView:CollectionFootView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "foot", for: indexPath) as! CollectionFootView
            return footerView
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值